Details about the template in the Python Django framework

Source: Internet
Author: User
This article mainly introduces the template-related knowledge in the Python Django framework. The existence of the template greatly simplifies HTML-related work during page creation, if you need it, you can refer to HTML which is directly hardcoded in Python code.

def current_datetime(request):  now = datetime.datetime.now()  html = "It is now %s." % now  return HttpResponse(html)

Although this technology is easy to explain how a view works, it is not a good idea to hardcode HTML directly into your view. Let's take a look at why:

  • Any changes to the page design must be made to the Python code. Site design changes are often more frequent than the underlying Python code changes. Therefore, it is much easier to change the design without modifying the Python code.
  • Python coding and HTML design are two different tasks. Most professional website development environments assign them to different people (or even different departments. Designers and HTML/CSS coders should not be asked to edit Python code to complete their work.
  • Programmers write Python code and designers make templates at the same time, it is far better than waiting for another person to finish editing a file that contains both Python and HTML.

For these reasons, the page design and Python code separation meetings are more clean, concise, and easy to maintain. We can use the Template System of Django to implement this mode. This is a specific issue to be discussed in this chapter.


Basic knowledge about the template System

A template is a text that is used to separate the representation and content of a document. A template defines placeholders and various basic logics (template tags) used to demonstrate how a document is displayed ). Templates are usually used to generate HTML, but Django templates can also generate any document based on text format.

Let's start with a simple example template. This template describes an HTML page to thank a person who signs a ticket with the company. It can be considered as a letter in the following format:

Ordering noticeOrdering notice

Dear {{ person_name }},

Thanks for placing an order from {{ company }}. It's scheduled toship on {{ ship_date|date:"F j, Y" }}.

Here are the items you've ordered:

    {% for item in item_list %}
  • {{ item }}
  • {% endfor %}
{% if ordered_warranty %}

Your warranty information will be 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 }}

This template is a basic HTML that adds some variables and template tags. Let's analyze it step by step:

The text enclosed in two braces (for example, {person_name}) is called a 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.

The text enclosed by braces and percent signs (for example, {% if ordered_warranty %}) is a template tag ). Tags are clearly defined, that is, labels that only notify the template system to complete some work.

The template in this example contains a for tag ({% for item in item_list %}) and an if tag ({% if ordered_warranty % })

The for tag is similar to the for statement in Python, allowing you to access every project in the sequence cyclically. The if tag, as expected, is used to perform logical judgment. Here, the tag label checks whether the value of ordered_warranty is True. If yes, the template System displays the content between {% if ordered_warranty %} and {% else %; otherwise, content between {% else %} and {% endif %} is displayed. {% Else %} is optional.

Finally, the second section of the template contains an example of the filter, which is the most convenient way to convert the variable output format. For example, {ship_date | date: "F j, Y"} in this example, we pass the variable ship_date to the date filter and specify the parameter "F j, Y ". The date filter outputs the format based on parameters. The filter is called using the pipeline operator (|). For more information, see Unix pipeline operator.

The Django template contains many built-in tags and filters, which will be learned one after another. appendix F lists many tags and filters lists. It is a good suggestion for you to familiarize yourself with these lists. you can still use it to create your own tag and filters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.