How do I check for last loop iteration in Django template?
You would use forloop.last. For example: <ul> {% for item in menu_items %} <li{% if forloop.last %} class=”last”{% endif %}>{{ item }}</li> {% endfor %} </ul>
You would use forloop.last. For example: <ul> {% for item in menu_items %} <li{% if forloop.last %} class=”last”{% endif %}>{{ item }}</li> {% endfor %} </ul>
You want the default_if_none template filter, (doc). default_if_none will display the given string if the variable is ‘None’. default will display the string if the variable evaluates to False, ie empty strings, empty lists etc {{ item.somefield|default_if_none:”” }} {{ item.somefield|default:”” }}
When you use the extends template tag, you’re saying that the current template extends another — that it is a child template, dependent on a parent template. Django will look at your child template and use its content to populate the parent. Everything that you want to use in a child template should be within … Read more
You should be able to concatenate strings with the add template filter: {% with ‘assets/flags/’|add:request.LANGUAGE_CODE|add:’.gif’ as image_static %} {% static image_static %} {% endwith %} What you are trying to do doesn’t work with the static template tag because it takes either a string or a variable only: {% static “myapp/css/base.css” %} {% static variable_with_path … Read more
None, False and True all are available within template tags and filters. None, False, the empty string (”, “”, “”””””) and empty lists/tuples all evaluate to False when evaluated by if, so you can easily do {% if profile.user.first_name == None %} {% if not profile.user.first_name %} A hint: @fabiocerqueira is right, leave logic to … Read more
{% block javascript %} {{ block.super }} … more content … {% endblock %} See: Django documentation – Template inheritance
Under your main views.py add your own custom implementation of the following two views, and just set up the templates 404.html and 500.html with what you want to display. With this solution, no custom code needs to be added to urls.py Here’s the code: from django.shortcuts import render_to_response from django.template import RequestContext def handler404(request, *args, … Read more
Use the Django template macros plugin: https://gist.github.com/1715202 (django >= 1.4) or http://www.djangosnippets.org/snippets/363/ (django < 1.4) django >= 1.4 # base.html {% kwacro title %} {% block title %}My Cool Website{% endblock %} {% endkwacro %} <html> <head> <title>{% usekwacro title %}</title> </head> <body> <h1>{% usekwacro title %}</h1> </body> </html> and # blog.html {% extends ‘base.html’ … Read more
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
It looks like {{ data.0 }}. See Variables and lookups.