PropertyBasedContext
This approach allows you to avoid code duplication to produce same context data for different views. It could be usefull when you are using templates inheritace.
- context
- contextprocessor
This approach allows you to avoid code duplication to produce same context data for different views. It could be usefull when you are using templates inheritace.
This is http://djangosnippets.org/snippets/1376/ rewritten as a new class-style Loader, and slightly improved. Allows you to reference templates like this: app_label:some/template/name.html This makes it possible to insert customizations at any point in a template hierarchy. For example, you could replace a block within the base admin template: {% extends "admin:admin/base.html" %} {% block breadcrumbs %}Custom Breadcrumbs-Style{% endblock %}
This snippet shows how to add additional parameters to the queryset of a ModelChoiceField, enabling you to for instance filter the query.
Get any value from settings.py as a template variable. The variable can then be used in conditional tags. E.g. to show a link to a help page only if it the help page url is defined in settings.py {% load get_setting %} {% get_setting MY_HELP_URL as help_url %} {% if help_url %}<a href="{% help_url|safe %}">Help</a>{% endif %}
When using Amazon's ELB with SSL termination, Django needs to know that the requests are secure. You need to use the special indication given on the request (HTTP_X_FORWARDED_PROTO) to determine that. This middleware will do that for you. The second part is forcing requests to HTTPS in case they are not. This part is not mandatory and could probably be done using configuration rules in your HTTP server. Note that I did it for the specific site, simply to avoid redirecting requests which are not of interest in the first place. This snippet is based on work done in [Django-Heroism](https://github.com/rossdakin/django-heroism).
A pre_save signal that will automatically generate a slug for your model based on the "title" attribute, and will store the new slug in the "slug" attribute. USAGE: from django.db.models.signals import pre_save from YOURPACKAGE import slug_generator pre_save.connect(slug_generator, sender=YOURMODEL)
In case when you need a list of related objects for particular queryset
This django field can be used to introduce Spanish national Identification Numbers (NIF). It validates the NIF format and checks if the last letter is correct. It also validates NIE numbers for foreign people. You can use the field in your own forms: 'code' dni = DNIField(max_length=15, widget=forms.TextInput(), label=_('DNI')) 'code'
I wanted a simple way to display events for a given month in a calendar, so I adapted some code to write a template tag. `Event` is the model class, and it needs to have a `date_and_time` field. Each event is part of a `Series`, so in the view you can filter events by which series, or display events for all known Series. [Full explanation and description of simple Django event calendar template tag](http://williamjohnbert.com/2011/06/django-event-calendar-for-a-django-beginner/).
Admin Image Widget(snippet 934) with easy-thumbnails
Ordinarily, it is not possible to edit Group membership from the Group admin because the m2m relationship is defined on User. With a custom form, it is possible to add a ModelMultipleChoiceField for this purpose. The trick is to populate the initial value for the form field and save the form data properly. One of the gotchas I found was that the admin framework saves ModelForms with commit=False and uses the save_m2m method when the group is finally saved. In order to preserve this functionality, I wrap the save_m2m method when commit=False.
Tightens up response content by removed superflous line breaks and whitespace. By Doug Van Horn ---- CHANGES ---- v1.1 - 31st May 2011 Cal Leeming [Simplicity Media Ltd] Modified regex to strip leading/trailing white space from every line, not just those with blank \n. ---- TODO ---- * Ensure whitespace isn't stripped from within <pre> or <code> or <textarea> tags.
PushPinImageField is a Django form field that is a sub-class of an ImageField. The field accepts an image to upload and based on certain settings, notably the size of the resulting image, the sizes and colors of 3 different borders, as well as the color of the push pin, a re-sized image is created with a colorized push pin in the top-center. A live demo of the field as well as more detailed usage instructions are available as a [blog entry](http://www.robmisio.com/blog/2/).
The django admin (as of 1.3) doesn't allow for short_description (and other ModelAdmin function attributes) to be a callable. Until that happens, this handy snippet allows you to return dynamic descriptions. Note, I haven't actually tested this yet -- but I plan to when I have the free time.
Sorl thumbs for the lazy. **Usage:** {% thumb p.image "noimage.png" %} *tags.py* goes into your templatetags directory. *includes/thumb.html* goes into your templates directory. [Fork it here.](https://gist.github.com/985439)