HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
In Django templates, you can render variables by putting them inside{{ }} brackets
In Django templates, you can render variables by putting them inside
{{ }}
brackets:
template.html
:
<h1>Hello {{ firstname }}, how are you?</h1>
The variable firstname
in the example above was sent to the template via a view:
views.py
:
from django.http import HttpResponse
from django.template import loader
def testing(request):
template = loader.get_template('template.html')
context = {
'firstname': 'Linus',
}
return HttpResponse(template.render(context, request))
As you can see in the view above, we create an object named context and fill it with data, and send it as the first parameter in the
template.render()
function.
You can also create variables directly in the template, by using the
{% with %}
template tag:
template.html
:
{% with firstname="Tobias" %}
<h1>Hello {{ firstname }}, how are you?</h1>
You will learn more about template tags in the next chapter.
The example above showed a easy approach on how to create and use variables in a template.
Normally, most of the external data you want to use in a template, comes from a model.
We have created a model in the previous chapters, called Members, we will use this model in the next chapters of this tutorial.
To get data from the Members model, we will have to import it in the views file, and extract data from it in the view:
views.py
:
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Members
def testing(request):
mymembers = Members.objects.all().values()
template = loader.get_template('template.html')
context = {
'mymembers': mymembers,
}
return HttpResponse(template.render(context, request))
Now we can use the data in the template:
template.html
:
<ul>
{% for x in mymembers %}
<li>{{ x.firstname }}</li>
{% endfor %}
</ul>
We use the Django template tag {% for %}
to loop through the
members.
template tags