“{% extends %}” vs “{% include %}” in Django Templates

Extending allows you to replace blocks (e.g. “content”) from a parent template instead of including parts to build the page (e.g. “header” and “footer”). This allows you to have a single template containing your complete layout and you only “insert” the content of the other template by replacing a block. If the user profile is … Read more

How do I display the value of a Django form field in a template?

This was a feature request that got fixed in Django 1.3. Here’s the bug: https://code.djangoproject.com/ticket/10427 Basically, if you’re running something after 1.3, in Django templates you can do: {{ form.field.value|default_if_none:”” }} Or in Jinja2: {{ form.field.value()|default(“”) }} Note that field.value() is a method, but in Django templates ()‘s are omitted, while in Jinja2 method calls … 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

Django foreign key relation in template

If you review the foreign key documentation, if you have a relationship like Doc -> has many DocImages you need to define your foreign key on the DocImages class like so: class DocImage(models.Model): property = models.ForeignKey(Doc, related_name=”images”) If you don’t set related names, you can access the DocImages from the Doc like: Doc.docimage_set.all() Docs on … Read more