Questions tagged [django-templates]

Questions about the template engine of Django, which is intended to separate the presentation of a document from its data.

A Django template is a string of text that is intended to separate the presentation of a document from its data. A template defines placeholders and various bits of basic logic — tags — that regulate how the document should be displayed. Usually, templates are used for outputting HTML but Django templates are equally capable of generating any text-based format.

The two key features of Django templates are that they are not, on their own, very powerful (in particular, no functions may be called with parameters), and a flexible, extensible tag system which allows users to break out of its constraints. This tends to prevent logic other than presentation logic from creeping into templates.

An example template built with the Django template language.

<html>
    <head>
        <title>{{ site.title }}</title>
    </head>
    <body>
        <h1>{{ site.greeting }}</h1>
        <ul>
            {% for item in menu_items %}
            <li>{{ item|upper }}</li>
            {% endfor %}
        </ul>
    </body>
</html>

See the official documentation for more details.

19491 questions
206
votes
24 answers

Iterate over model instance field names and values in template

I'm trying to create a basic template to display the selected instance's field values, along with their names. Think of it as just a standard output of the values of that instance in table format, with the field name (verbose_name specifically if…
Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
199
votes
8 answers

How can I check the size of a collection within a Django template?

I have a list in my Django template. I want to do something only if the size of the list is greater than zero. I have tried myList|length and myList|length_is but they have not been successful. I've searched all over and don't see any examples.…
MrDatabase
  • 43,245
  • 41
  • 111
  • 153
176
votes
4 answers

how to iterate through dictionary in a dictionary in django template?

My dictionary looks like this(Dictionary within a dictionary): {'0': { 'chosen_unit': , 'cost': Decimal('10.0000'), 'unit__name_abbrev': u'G', 'supplier__supplier': u"Steve's Meat Locker", 'price': Decimal('5.00'), …
darren
  • 18,845
  • 17
  • 60
  • 79
173
votes
6 answers

How to add url parameters to Django template url tag?

In my view to get url parameters like this: date=request.GET.get('date','') In my url I am trying to pass parameters in this way with the url template tag like this:
Atma
  • 29,141
  • 56
  • 198
  • 299
168
votes
12 answers

How to override and extend basic Django admin templates?

How do I override an admin template (e.g. admin/index.html) while at the same time extending it (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template)? First - I know that this question has been…
Semmel
  • 2,526
  • 3
  • 21
  • 30
168
votes
5 answers

Django TemplateSyntaxError - 'staticfiles' is not a registered tag library

After upgrading to Django 3.0, I get the following TemplateSyntaxError: In template /Users/alasdair//myproject/myapp/templates/index.html, error at line 1 'staticfiles' is not a registered tag library. Must be one of: admin_list admin_modify…
Alasdair
  • 298,606
  • 55
  • 578
  • 516
156
votes
7 answers

Make the first letter uppercase inside a django template

I am pulling a name from a database which is stored as myname. How do I display this inside a Django template as Myname, with the first letter being in uppercase.
Mayank
  • 2,333
  • 3
  • 17
  • 23
153
votes
5 answers

Reference list item by index within Django template?

This may be simple, but I looked around and couldn't find an answer. What's the best way to reference a single item in a list from a Django template? In other words, how do I do the equivalent of {{ data[0] }} within the template language?
user456584
  • 86,427
  • 15
  • 75
  • 107
153
votes
8 answers

Creating a dynamic choice field

I'm having some trouble trying to understand how to create a dynamic choice field in django. I have a model set up something like: class rider(models.Model): user = models.ForeignKey(User) waypoint = models.ManyToManyField(Waypoint) class…
whatWhat
  • 3,987
  • 7
  • 37
  • 44
152
votes
7 answers

Is it possible to pass query parameters via Django's {% url %} template tag?

I'd like to add request parameters to a {% url %} tag, like ?office=foobar. Is this possible? I can't find anything on it.
Brian D
  • 9,863
  • 18
  • 61
  • 96
150
votes
3 answers

How to access outermost forloop.counter with nested for loops in Django templates?

Is it possible to access the forloop.counter for the outermost for loop in the following template in Django: {% for outerItem in outerItems %} {% for item in items%}
{{ forloop.counter }}. {{ item }}
{% endfor %} {%…
jamesaharvey
  • 14,023
  • 15
  • 52
  • 63
149
votes
12 answers

How can I change the default Django date template format?

I have dates in ISO 8601 format in the database, %Y-%m-%d. However, when the date is passed on to the template, it comes out as something like Oct. 16, 2011. Is there a way that I can manipulate the format to whatever I want?
bash-
  • 6,144
  • 10
  • 43
  • 51
145
votes
20 answers

Django, creating a custom 500/404 error page

Following the tutorial found here exactly, I cannot create a custom 500 or 404 error page. If I do type in a bad url, the page gives me the default error page. Is there anything I should be checking for that would prevent a custom page from showing…
reZach
  • 8,945
  • 12
  • 51
  • 97
142
votes
15 answers

How to repeat a "block" in a django template

I want to use the same {% block %} twice in the same django template. I want this block to appear more than once in my base template: # base.html {% block title %}My Cool Website{% endblock %}
David Arcos
  • 5,957
  • 5
  • 30
  • 39
142
votes
9 answers

What is the equivalent of "none" in django templates?

I want to see if a field/variable is none within a Django template. What is the correct syntax for that? This is what I currently have: {% if profile.user.first_name is null %}

--

{% elif %} {{ profile.user.first_name }} {{…
goelv
  • 2,774
  • 11
  • 32
  • 40