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