HTML is hard-coded directly into Python code.
def current_datetime (Request): Now = Datetime.datetime.now () html = "It's now%s."% now return HttpResponse (HTML)
While this technique makes it easy to explain how views work, it's not a good idea to hardcode html directly into your view. Let's take a look at why:
- Any changes to the design of the page must be modified accordingly to the Python code. Site design modifications are often much more frequent than the underlying Python code, so it would be much easier to change the design without Python code modifications.
- Python code writing and HTML design are two different tasks, and most professional web development environments assign them to different people (or even different departments) to do so. Designers and Html/css coders should not be asked to edit Python's code to do their job.
- Programmers writing Python code and designer templates two work at the same time is the most efficient, far better than letting one person wait for another to finish editing a file that contains both Python and HTML.
For these reasons, separating the design of the page from the Python code will be more clean, concise and easier to maintain. We can use the Django template system to implement this pattern, which is the problem that this chapter will discuss specifically.
Basic knowledge of template system
A template is a text that separates the representation and content of a document. The template defines the placeholder and various basic logic (template tags) that are used to standardize how the document should be displayed. Templates are often used to generate HTML, but Django templates can also produce any document based on text format.
Let's start with a simple example template. The template describes a thank you HTML page to a company signing person. It can be treated as a form letter:
Ordering NoticeOrdering Notice
Dear {{Person_name}},
Thanks for placing a order from {{company}}. It ' s scheduled toship on {{ship_date|date: "F J, Y"}}.
Here is the items you ' ve ordered:
{% for item in item_list%}
- {{Item}}
{% endfor%}
{% if Ordered_warranty%}
Your Warranty information'll be a included in the packaging.
{% Else%}
You didn ' t order a warranty, so you ' re on your own when the products inevitably stop working.
{% ENDIF%}
Sincerely,
{{company}}
The template is a piece of underlying HTML that adds some variables and template tags. Let's step through the analysis:
Text enclosed in two curly braces (for example, {{person_name}}) is called a variable (variable). This means that the value of the specified variable is inserted here. How do I specify the value of a variable? It will be explained later.
Text enclosed by curly braces and a percent semicolon (for example, {% if Ordered_warranty%}) is a template tag. The label (tag) definition is clear: only the label that informs the template system to do some work.
The template in this example contains a for label ({% for item in item_list%}) and an if label ({% if ordered_warranty%})
The for tag is similar to a Python for statement, allowing you to iterate through each item in the sequence. The IF tag, as you expected, is used to perform logical judgments. Here, the tag tag checks if the Ordered_warranty value is true. If so, the template system will display the contents between {% if Ordered_warranty%} and {% Else%}, otherwise the contents between {% Else%} and {% ENDIF%} will be displayed. {% Else%} is optional.
Finally, in the second paragraph of this template, there is an example of filter filters, which is one of the most convenient ways to convert variable output formats. As in this example, {{ship_date|date: "F J, Y"}}, we pass the variable ship_date to the date filter, specifying the parameter "F j,y". The date filter formats the output according to the parameters. The filter is with a pipe character (|) To invoke, see the UNIX pipe character for details.
Django templates contain a lot of built-in tags and filters, we will continue to learn. Appendix F lists a lot of tags and filters lists, and familiarity with these lists is a good idea for you. You can still use it to create your own tag and filters.