aldoridhoni / django-tekextensions

Re-usable views/templatetags/widgets for django applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tekextensions is made for Django 1.9+.

admin popups

if using grappeli, copy RelatedObjectLookups.js to your admin_media folder from django's admin media

settings.py

tekextensions relies on the Django static package.

STATIC_ROOT = ...
STATIC_URL = ...
STATICFILES_DIRS = ...
STATICFILES_FINDERS = ..._
TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'tekextensions.context_processors.static_url_prefix',
            ]
        }
     }
]
INSTALLED_APPS = [
    ...
    'tekextensions',
]

Fill in the STATIC variables as appropriate.

urls.py

from teksextensions import views as t_views
url(r'^add/(?P<model_name>\w+)/?$', t_views.add_new_model, name='popup'),

forms.py

override any ModelChoiceField widget with SelectWithPopUp

from tekextensions.widgets import SelectWithPopUp
from django import forms
from .models import MyModel, AnotherModel

class CustomForm(forms.Form):
    company = forms.ModelChoiceField(CustomModel.objects, widget=SelectWithPopUp)

class CustomModelForm(forms.ModelForm):
    class Meta:
        model=MyModel
        widgets = {
            'field1': SelectWithPopUp,
            'field2': SelectWithPopUp(model='AnotherModel')
        }

If you decide to specify the template argument to SelectWithPopup(), your template will need to contain the following in order to create the green plus sign link just as Django Admin does:

{% load static %}
<a href="{% url 'popup' model_name=model %}/" class="add-another" id="add_id_{{ field }}" onclick="return showAddAnotherPopup(this);">
<img src="{% static 'admin/img/icon_addlink.gif' %}">
</a>

The model and field values will be filled in automatically as the template is rendered.

html

create HTML template for the popup

By default, the template is named popup.html. This can be overridden by sending in the template name via the url. For example:

url(r'^add/(?P<model_name>\w+)/?$', tekextensions.views.add_new_model, name='popup, {'template': 'my_template'})

An example of a simple template:

{% load staticfiles %}

<html>
    <head>
        <title>Add {{ field }}</title>
        <script src="{% static 'admin/js/admin/RelatedObjectLookups.js' %}"></script>
    </head>

    <body>
        <h1>Add {{ field }}</h1>

        <form method="POST" action="/add/{{ field }}">
            {% csrf_token %}

            <table>
                {{ form.as_table }}
            </table>

            <div style="margin-top: 1em;">
                <input type="submit" value="Add" /> | <a href="javascript:window.close()">Cancel</a>
            </div>
        </form>
    </body>
</html>

About

Re-usable views/templatetags/widgets for django applications


Languages

Language:Python 71.3%Language:JavaScript 20.2%Language:HTML 8.5%