Django tutorial: Get began with Django 6

Learn extra at:

Yr: {{yr}}

Any worth inside double curly braces in a template is handled as a variable. All the pieces else is handled actually.

Modify myapp/views.py to appear like this:


from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hey, world!")

def yr(request, yr):
    information = {'yr':yr}
    return render(request, 'myapp/yr.html', information)

The render perform—a Django “shortcut” (a mix of a number of built-ins for comfort)—takes the present request object, appears for the template myapp/yr.html within the record of accessible template areas, and passes the dictionary information to it as context for the template. The template makes use of the dictionary as a namespace for variables used within the template. On this case, the variable {{yr}} within the template is changed with the worth for the important thing yr within the dictionary information (that’s, information["year"]).

The quantity of processing you are able to do on information inside Django templates is deliberately restricted. Django’s philosophy is to implement the separation of presentation and enterprise logic at any time when doable. Thus, you may loop by means of an iterable object, and you may carry out if/then/else exams, however modifying the info inside a template is discouraged.

Leave a reply

Please enter your comment!
Please enter your name here