Django templates: verbose version of a choice

In Django templates you can use the “get_FOO_display()” method, that will return the readable alias for the field, where ‘FOO’ is the name of the field. Note: in case the standard FormPreview templates are not using it, then you can always provide your own templates for that form, which will contain something like {{ form.get_meal_display … Read more

Sorting a Django QuerySet by a property (not a field) of the Model

Use QuerySet.extra() along with CASE … END to define a new field, and sort on that. Stops.objects.extra(select={‘cost’: ‘CASE WHEN price=0 THEN 0 ‘ ‘WHEN type=:EXPRESS_STOP THEN price/2 WHEN type=:LOCAL_STOP THEN price*2’}, order_by=[‘cost’]) That, or cast the QuerySet returned from the rest to a list, then use L.sort(key=operator.attrgetter(‘cost’)) on it.

Django template counter in nested loops

I found a better solution with itertools. (Better than my previous answer) You can set current state of the loop to the itertools variable sent to the view context. This time i tried on a dummy Django project and it works like a charm. views.py: from django.shortcuts import render_to_response import itertools def home(request): iterator=itertools.count() base_parts … Read more

Get type of Django form widget from within template

Making a template tag might work? Something like field.field.widget|widget_type Edit from Oli: Good point! I just wrote a filter: from django import template register = template.Library() @register.filter(‘klass’) def klass(ob): return ob.__class__.__name__ And now {{ object|klass }} renders correctly. Now I’ve just got to figure out how to use that inside a template’s if statement. Edit … Read more

How to redirect with messages to display them in Django Templates?

request.user.message_set was deprecated in Django 1.2 and has been removed since Django 1.4, the message framework should be used instead. from django.contrib import messages # messages.add_message(request, level, message, extra_tags=””, fail_silently=False) messages.add_message(request, messages.INFO, “Your Message”) Alternatively, you can use one of the shortcut functions: from django.contrib import messages messages.debug(request, “Your Message”) messages.info(request, “Your Message”) messages.success(request, “Your … Read more

Reverse for ‘*’ with arguments ‘()’ and keyword arguments ‘{}’ not found

There are 3 things I can think of off the top of my head: Just used named urls, it’s more robust and maintainable anyway Try using django.core.urlresolvers.reverse at the command line for a (possibly) better error >>> from django.core.urlresolvers import reverse >>> reverse(‘products.views.filter_by_led’) Check to see if you have more than one url that points … Read more

How do I use Django templates without the rest of Django?

The solution is simple. It’s actually well documented, but not too easy to find. (I had to dig around — it didn’t come up when I tried a few different Google searches.) The following code works: >>> from django.template import Template, Context >>> from django.conf import settings >>> settings.configure() >>> t = Template(‘My name is … Read more