Python Learning---django template inheritance 180123

Source: Internet
Author: User

Django Template Inheritance--20180123

A.include Template Tags

B.extend (inheritance) template label

-- include template label

This tag allows the content of other templates to be included in the (template). The parameter of the label is the name of the template to be included, either a variable or a hard-coded string in single/double quotation marks. Whenever the same code appears in multiple templates, you should consider whether you want to use {% include%} to reduce duplication.

------extend (inheritance) template label

You first construct an underlying framework template and then overload it with the common parts of the site and the definition blocks in its child templates.

settigs.py

' DIRS ': [Os.path.join (Base_dir, ' templates ')],  # Set templates path to Django previous version # ' DIRS ': [],      # comment out the line, this is the Django 2.0.1 Latest Version # ' Django.middleware.csrf.CsrfViewMiddleware ',         ... Omit default configuration Static_url = '/static/' Template_dirs = (Os.path.join (base_dir,  ' templates '),)  # original config # static resource file Staticfiles _dirs = (Os.path.join (base_dir, "statics"),)   # Now add the configuration, here is the tuple, note the comma

Templates/base.html

<! DOCTYPE html>

Templates/order.html

{% extends "base.html"%}  {# This inherits the base class and must be placed in the first row #{% block content%}       {# Fill in the new content according to the name of the Block #}     <div class= "Content" >        order    </div>{% endblock%}

Templates/shopping.html

{% extends "base.html"%} {# This inherits the base class and must be placed in the first line #} {% block content%}       {# Fill in the new content according to the name of the Block #}     <div class= "Content" >        shopping cart    </div>{% endblock%}

mysite2/urls.py

From Django.contrib import adminfrom django.urls import pathfrom Blog import viewsfrom django.conf.urls import Urlurlpatte RNS = [      # Order    --order URL (r ' order/', Views.order), # Shopping  --Shopping Cart URL (r ' shopping/', views.shopping),]

views.py

From django.shortcuts import Render, Httpresponseimport datetime # Orderdef Order (Request):    return render (Request, " Order.html ") # Shoppingdef Shopping (Request):    return render (Request," shopping.html ")

Page display:

Attention:

We can get the contents of the parent class by {% Block.super%}

<1> Create a base.html template in which to define the main visual feel of the site. These are parts that are infrequently modified or even never modified.

<2> Create base_section.html templates for each area of the site these templates extend the base.html and include area-specific styles and designs.

<3> create separate templates for each type of page, such as forum pages or photo galleries. These templates extend the appropriate area templates.

Python Learning---django template inheritance 180123

Related Article

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.