Reputation: 131
So I'm new to Django and HTML, but not to python. I'm looking for a little clarification on the syntax used inside HTML files.
Why is HTML sometimes written as:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
as opposed to using plain HTML tags inside of these .html files?
I'm just confused by the "{%...%} tags..Can someone elaborate on what exactly happens when you use them vs plain HTML?
Upvotes: 0
Views: 415
Reputation: 3088
The {% %}
and {{ }}
pairs is used in the template system.
If you want to display, say user.email
in a template, you would send this information from your view to your template in a dict
we usually call the context
, and then the template will have access using {{ user.email }}
.
The other is used for program constructs, say:
{% if user %}
<h1>Hi {{ user.firstname }}</h1>
{% else %}
<h1>You must log in.</h1>
{% endif %}
As you can see it's not real Python, but some Pythonish code allowed in templates.
Upvotes: 2
Reputation: 372
{% %} these tags allow you perform logical operations to your html file. You can pass variables from the python view file to the template file and then you can have apply logic based on those variables using {% %} tag.
You can learn more about Django templates in here
Upvotes: 3