Django Template language

Source: Internet
Author: User
Tags comment tag

The purpose of the Django template language is to strike a balance between strength and ease of use, and the Django template language is simpler and more exclusive than other template languages.

Django template system consists of templates, variables, filters, tags, annotations and other major parts

Template

A template is a simple text file that can generate any text-based format, html,csv,xml, etc.

The template contains variables, filters, tags and notes, and here is a simple template file

{% extends "base_generic.html"%}{% block title%}{{section.title}}{% endblock%}{% block content%}<H1>{{Section.title}}</H1>{% for stories in story_list%}<H2>  <ahref= "{{Story.get_absolute_url}}">{{Story.headline|upper}}</a></H2><P>{{story.tease|truncatewords: ' 100 '}}</P>{% endfor%}{% endblock%}

Variable

Variable format:{{ variable }}

Variable resolution: When the template engine encounters a variable, it calculates the variable and replaces it with the result.

Name of the variable: combination of alphanumeric and underscore, (dot) not available because Dot has a special meaning

{{variable}} will be parsed as "(an empty string) if the variable does not exist.

Filter filters

Modify how variables are displayed

Format:{{ variable|filter }}, showing the result of the variable after the filter, for example, {{ name|lower }} The lowercase form of the tactical name, | is the pipeline

Filter can be concatenated, for example {{ text|escape|linebreaks }}

Filter can have parameters, denoted by a colon, for example {{ bio|truncatewords:30 }}

The filter parameter must be enclosed in quotation marks if it has a space, for example:{{ list|join: ", " }}

Django has a lot of built-in filters, and we'll talk about it in the next section, because there are 30 more, enough to be a section.

Label

Tags are more complex than variables and filters, some of which produce content on the output, some control text flow (through loops and logic), and some that load external content into the template

Format: {% tag%}

The tags are basically paired, such as:{% tag %} ... tag contents ... {% endtag %}

Django has a lot of built-in tags, close to 40, enough to be a section, next to the next section.

Comments

Note, don't explain it.

Format:{# #}

But this comment tag can only comment on a single line of text

If you want to annotate multiple lines, go through the Comment tab

Template inheritance

Template system is the most powerful and most complex part-template inheritance, you can use the block tag in the parent template to define some of the column variables, and then overwrite them in the Word template, see the following example:

Base.html

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <Linkrel= "stylesheet"href= "Style.css" />    <title>{% block title%} My amazing site{% endblock%}</title></Head><Body>    <DivID= "sidebar">{% block sidebar%}<ul>            <Li><ahref="/">Home</a></Li>            <Li><ahref= "/blog/">Blog</a></Li>        </ul>{% Endblock%}</Div>    <DivID= "Content">{% block content%}{% endblock%}</Div></Body></HTML>

A sub-template is as follows, the key is extends label

{% extends "base.html"%}{% block title%}my amazing blog{% endblock%}{% block content%}{% for entry in Blog_entries %}    <H2>{{entry.title}}</h2>    < P > {{Entry.body}} </ P > {% endfor%}{% endblock%}

The effect of rendering is as follows: Some tags in the child template will overwrite the contents of the parent template, such as title, and the tags that the child template does not overwrite will inherit from the parent template, such as sidebar.

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <Linkrel= "stylesheet"href= "Style.css" />    <title>My Amazing Blog</title></Head><Body>    <DivID= "sidebar">        <ul>            <Li><ahref="/">Home</a></Li>            <Li><ahref= "/blog/">Blog</a></Li>        </ul>    </Div>    <DivID= "Content">        <H2>Entry One</H2>        <P>This is my first entry.</P>        <H2>Entry</H2>        <P>This is my second entry.</P>    </Div></Body></HTML>

Here are some tips:

    • If you want to use the extends tag, make sure that the first label is extends in the child template
    • The more block tags in the parent template, the better
    • If you have duplicate content in multiple sub-templates, it's time to consider putting them in the parent template.
    • {{ block.super }} can access the value of the corresponding block in the parent template, which may be used when you simply want to add content to the parent template rather than overwrite it.
Automatic HTML escape

Threats primarily from cross-site attacks (XXS)

Obviously, user submissions can not be blindly trusted, because some of the malicious users may use this blind trust to do something you do not expect, such as embedded in the content of a piece of JavaScript code and so on, so Django is automatically escaped by default, that is, the following conversion

    • < is converted to &lt;
    • > is converted to &gt;
    • ' (single quote) are converted to & #39;
    • " (double quote) is converted to &quot;
    • & is converted to &amp;

You can turn off automatic HTML escaping if you really want to give up automatic escaping, or if you have your own needs, or if you are sure that the user's input is not compromised

In the variable, you can do this

This would be escaped: {{Data}}this is not being escaped: {{data|}}

In the label, you can do this

autoescape off %} This is not being    auto-escaped: {{data}}.    Nor this: {{other_data}}    autoescape on%}        auto-escaping applies again: {{name}}    {% Endautoescape %}{% Endautoescape%}

Accessing method calls

Most of the method calls are still available in the template, but the method is not defined in the template, and all property methods must be defined before the template.

{% for comment in task. comment_set.all %}    {{comment}}{% endfor%}{{task.  Comment_set.all.count }}# in Modelclass Task (models. Model):    def foo (self):        return ' Bar ' # in template{{task.  Foo }}

P

Django Template language

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.