Django template system learning

Source: Internet
Author: User

Differences and usage of block extends include

1. define the basic template. Define multiple block blocks in the HTML content. The block is determined by whether to replace these blocks by referencing block blocks with the same name in the subtemplate.
{% Block title %} some content, which is not specified here {% endblock %}
{% Block content %} some content, which is not specified here {% endblock %}
{% Block footer %} some content, which is not specified here {% endblock %}

Here title content footer is not a variable and its name is customized

Blockgeneral definition in base.html, block is the area where content can be added to the HTML that inherits the base.



Ii. subtemplate Reference Method
{% Extends "base.html" %}
{% Block title %} the current time {% endblock %}
{% Block content %} <p> it is now {current_date }}. </P> {% endblock %}

The first sentence is a fixed format and must be the first template tag in the template.
Extends parameters are generally strings and can be variables.
The portable path and relative path are based on the template directory of template_dirs.
The sub-template determines the block to be replaced, so you do not need to pay attention to other parts. If there is no defined block, you do not need to replace it. Instead, you can directly use the block of the parent template.


3. reference the upper-level code block and make some modifications based on It {block. Super }}
{% Block footer %}
{Block. Super }}
Aaaaa
{% Endblock %}





Django template system-the template contains another template include

After using the template to load the API mechanism, other template tags are available.
{% Include 'nav.html '%}
{% Include "nav.html" %}

The portable path and relative path are based on the template directory of template_dirs.
{% Include 'endpoints/nav.html '%}

Variable name
{% Include template_name %}
All included variables are processed in a unified manner, with no distinction between the layer-7 templates

Django {block. Super} template tag
The {block. Super} tag in the Django template is very useful. It can not only overwrite the parent template, but also Append content based on the parent template. Of course, it can also be overwritten.

This gives us flexibility: it can be completely rewritten, reused, or extended based on reuse.



2. Locals () Skills

Locals () returns a dictionary containing all the variables and their values in the current scope.

From Django. Shortcuts import render_to_response
Import datetime
Def current_datetime6 (request ):
Current_date = datetime. datetime. Now ()
Return render_to_response('current_datetime6.html ', locals ())

Django template system learning

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.