Template Template introduced structural optimization -include

Premise: Why do we need to introduce a template?

For example: It may be only a partial change in the page, the head and tail are fixed, so when local changes, switching the address, which is switch to another page

In this case only a partial code is changed, the head and tail of the code is the same, the same code is copied to the head and tail of the other sub-pages, then it is not a good way

Therefore, the introduction of the template is used

The first step: first create a scaffold of django

 

 

 Step 2: Create three test pages in templates 

The third step: to render the page in the view.py


Step four: do mapping in urls.py

 

Step five: to be a simple three page styles

You can see the head and tail of these three pages are the same code behind if there are multiple pages, then you are in trouble

At this point, we can create two pages

The same as the code used to hold the header and footer

Figure:

Then use include the introduction of these header.html and footer.html index.html and company.html and school.html

Input in the browser: mapping the corresponding address can be used to see that the template

E.g:

 

Even if you're done here!

 

Added: passing parameters of the problem -> small BB, directly on chestnuts

1. I added this parameter in the map view function index.html

So, there is no doubt in my index.html page {{name}} is a problem too 

But if I add in the head in the same header.html 

Well, the name rendering data can only be displayed in the index.html page, this parameter is not present company.html and school.html in, so it will show an empty value

Workaround: Use with transfer in include template company.html and school.html in a custom parameter to 

So get away !!!!!!

Hand is not easy, Leia!

Guess you like

Origin www.cnblogs.com/ifdashui/p/11797725.html