In another approach, I want to generate "Real" part. Now you have to use a template system offered by Django. Finally, after he is. Once it is. From reading the fourth chapter DB learned that in templates: access to variables is obtained by writing
{{varname}}
, code blocks are marked as {% instructions for making
%} and we have access to the filters (using the tries to check and show time soon.
Another curiosity that offer templates used in Django is able to generate a template object by specifying a string containing the template (except that of course is the ability to specify the file containing the template, but it is standard). I start the classic tests: . / Manage.py shell and then
: from django.template
import Template, then t = Template ("I'm {{name}}. {{Name}} {{name}}")
and
print t
which will only show us where the t. object to what they write in DB templates that are pre-compiled to an internal form, ready to generate the page. Therefore, the error in the template (eg t = Template ('{% user%} had errors ")
), revealed at the time of the appointment of a template object to life.
Generating the template takes place in a context that is represented by an object class Context
. In this short presentation code (the code directly from your sticky, I thought that this form of presentation will be better, of course, write what is after the colon):
In [27]: django.template from import Context, Template In [28]: t = Template ("I'm {{name}}. {{name}} {{name}}")
In [29]: c = Context ({' Name: "Blond", "name": "James"})
In [30]: t.render (c)
Out [30]: 'I'm Blond. James Blond '
course we see what happens, as context will have defined a variable:
In [33]: from django.template import Context, Template
In [34] : t = Template ("I'm {{surname }}.{{ name}} {{name}}")
In [35]: c = Context ({"name": "Blond"})
In [36]: t.render (c)
Out [36]: 'I'm Blond.Blond'"" long string "" "
As you can see , nothing bad will not happen. Generated an empty string. In my opinion, is a plus (CakePHP generate, if I remember correctly, the error in such cases). Page designer can so easily extend it with new elements that relate to variables not yet defined and not wait for the programmer.
last element in this entry is to enable interactive testing wielolinijkowych templates (useful information for those who have not benefited Python), multiline strings placed just between the triple quotes
:
In [39]: from django.template import Context, Template In [40]: t = Template ("" "I'm {{surname }}.{{ name}} {{name}}
....: The latest film I had {{}} liczbaZyc live
....: How about it "" ")
In [42]: c = Context ({" name ":" Blonde "," name ":" James "," liczbaZyc ": 5})In [43]: t.render (c)
DjangoBook fourth chapter is quite big, so its contents extend to the few entries in which I gradually expanded information about the templates.
Out [43]: 'I'm Blond.JamesBlond \\ nIf I have a new movie 5 live \\ nco you so '
Examples include repetition (Imports, re-definitions of objects) only to show the full picture of what is happening in the program. During one session, of course, there is no need to import multiple classes or creating the same template.
0 comments:
Post a Comment