mdemont
mdemont

Reputation: 107

Django Tutorial Part 3 - Code Clarification

Django newbie here. I am doing the Django tutorial and although I could blindly follow the code I am just confused as to how this works. I get that the {% code %} is what allows us to insert python code into the html, what I don't get is the line:

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}

I'd just like to have an explanation of how the two curly brackets with a variable translates to the string representation of that variable.

I'm asking because I tried doing a similar operation in the interpreter on my own and didn't get the same results.

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}
</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

Upvotes: 1

Views: 53

Answers (1)

alecxe
alecxe

Reputation: 474111

The {{ variable }} syntax in Django templates is used as a placeholder for a variable.

If you want to check how it works on the console, start the django shell:

$ django-admin shell
>>> from django.template import engines
>>>
>>> django_engine = engines['django']
>>> template = django_engine.from_string("Hello {{ name }}!")
>>> context = {'name': 'John'}
>>> template.render(context)
Hello John!

Upvotes: 2

Related Questions