django framework Learning: Six include the use of templates

Foreword

When we open a website, found that the top and bottom of each page of content is almost, in order to improve the reusability of code, we can use these head, the bottom of the function code is encapsulated into a single utility, similar to the python in the package, use when calling on it, django there similar functionality, include realized.

Public content 

Each page of the site generally, the top navigation bar, middle of the page content, there are some links at the bottom. During the design process we can put these separate code.

hello / templates / top.html separate out

<section>

<h1>顶部导航

</h1>

<p>python自动化-北京-流水</p> <hr>

</section>

hello / templates / end.html separate out

<section>

<br><br><br><br><hr>

<h1>底部导航</h1>

<p>底部一些友情链接啊</p>

</section>

hello / templates / base.html content

!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

<section>

< H1 of> top navigation </ h1 of>

< The p-> Python Automation - Beijing - water </ the p-> < HR>

</section>

<section>

< H1 of> body text </ h1 of>

< P> text content </ P>

</section>

<section>

<br>

<br>

<br>

<br>

<hr>

< H1 of> bottom navigation </ h1 of>

< P> bottom number link to ah </ P> </ sectionTop>

</body>

</html>

include grammar

hello/templates/page1.html

 

 hello / views.py view function

 

urls.py add the access path

 

Access address browser http://127.0.0.1:8000/page1/will be able to see the effect of the

Arguments

Top.html common part and which can be passed end.html variables, such as

<section>

< H1 of> top navigation </ h1 of>

< - P> Python automation {{name}} </ P> < HR>

</section>

Corresponding to the view function

def page1(request):

       context = {"name": "流水"}

  return render(request, 'page1.html', context)

Guess you like

Origin www.cnblogs.com/liushui0306/p/12553551.html