Django tutorial: Get began with Django 6

Yr: {{12 months}}

Any worth inside double curly braces in a template is handled as a variable. Every part 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("Howdy, world!")

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

The render perform—a Django “shortcut” (a mixture of a number of built-ins for comfort)—takes the present request object, seems for the template myapp/12 months.html within the listing 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 {{12 months}} within the template is changed with the worth for the important thing 12 months 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 potential. Thus, you’ll be able to loop by way of an iterable object, and you’ll carry out if/then/else assessments, however modifying the information inside a template is discouraged.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles