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
439
votes
17 answers

Can I access constants in settings.py from templates in Django?

I have some stuff in settings.py that I'd like to be able to access from a template, but I can't figure out how to do it. I already tried {{CONSTANT_NAME}} but that doesn't seem to work. Is this possible?
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
391
votes
16 answers

How to get the current URL within a Django template?

I was wondering how to get the current URL within a template. Say my current URL is: .../user/profile/ How do I return this to the template?
dotty
  • 40,405
  • 66
  • 150
  • 195
382
votes
7 answers

Django - iterate number in for loop of a template

I have the following for loop in my django template displaying days. I wonder, whether it's possible to iterate a number (in the below case i) in a loop. Or do I have to store it in the database and then query it in form of days.day_number? {% for…
orschiro
  • 19,847
  • 19
  • 64
  • 95
352
votes
4 answers

Django: Display Choice Value

models.py: class Person(models.Model): name = models.CharField(max_length=200) CATEGORY_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES) …
Shankze
  • 3,813
  • 3
  • 17
  • 11
336
votes
20 answers

Numeric for loop in Django templates

How do I write a numeric for loop in a Django template? I mean something like for i = 1 to n
Lev
  • 6,487
  • 6
  • 28
  • 29
310
votes
19 answers

How to use the variables from "views.py" in JavasScript, "" in Django Template?

When I render a page using the Django template renderer, I can pass in a dictionary variable containing various values to manipulate them in the page using {{ myVar }}. Is there a way to access the same variable in Javascript,
AlMcLean
  • 3,459
  • 2
  • 19
  • 14
308
votes
12 answers

AngularJS with Django - Conflicting template tags

I want to use AngularJS with Django however they both use {{ }} as their template tags. Is there an easy way to change one of the two to use some other custom templating tag?
Endophage
  • 21,038
  • 13
  • 59
  • 90
298
votes
9 answers

How to set a value of a variable inside a template code?

Say I have a template
Hello {{name}}!
While testing it, it would be useful to define the value of the variable without touching the python code that invokes this template. So I'm looking for something like this {% set…
Alexis
  • 2,983
  • 2
  • 17
  • 5
275
votes
6 answers

How to put comments in Django templates?

I would like to comment this with a line: {% if something.property %} ... {% # this is a comment %} {% if something.property %}
...
Alex. S.
  • 143,260
  • 19
  • 55
  • 62
242
votes
31 answers

Django TemplateDoesNotExist?

My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these…
jack
  • 17,261
  • 37
  • 100
  • 125
239
votes
7 answers

Rendering a template variable as HTML

I use the 'messages' interface to pass messages to user like this: request.user.message_set.create(message=message) I would like to include html in my {{ message }} variable and render it without escaping the markup in the template.
xpanta
  • 8,124
  • 15
  • 60
  • 104
235
votes
15 answers

How to concatenate strings in django templates?

I want to concatenate a string in a Django template tag, like: {% extend shop/shop_name/base.html %} Here shop_name is my variable and I want to concatenate this with rest of path. Suppose I have shop_name=example.com and I want result to extend…
Ahsan
  • 11,516
  • 12
  • 52
  • 79
225
votes
9 answers

How to access a dictionary element in a Django template?

I would like to print out the number of votes that each choice got. I have this code in a template: {% for choice in choices %} {{choice.choice}} - {{votes[choice.id]}}
{% endfor %} votes is just a dictionary while choices is a model…
Mohamed
  • 3,420
  • 7
  • 28
  • 31
214
votes
19 answers

How to get the domain name of my site within a Django template?

How do I get the domain name of my current site from within a Django template? I've tried looking in the tag and filters but nothing there.
209
votes
19 answers

How to get a favicon to show up in my django app?

I just want to drop the favicon.ico in my staticfiles directory and then have it show up in my app. How can I accomplish this? I have placed the favicon.ico file in my staticfiles directory, but it doesn't show up and I see this in my log: 127.0.0.1…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
1
2 3
99 100