The template

Create a template

  • Create the template index.html for applying the view index under BookTest with the following directory structure:

  • Set the path to find TEMPLATES: Open the test1/settings.py file and set the DIRS value for TEMPLATES
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Copy the code

Define the template

  • Open templtes booktest/index. The HTML files, custom code is as follows
  • The syntax for output variables in a template is as follows. Variables may be passed from the view or defined in the template
{{variable name}}Copy the code
  • The syntax for writing code snippets in templates is as follows
{% code segment %}Copy the code
  • Define the template as follows
< HTML > < head > < title > book list < / title > < / head > < body > < h1 > {{title}} < / h1 > {% for I list in %} {{I}} < br > {% endfor %} < / body > </html>Copy the code

View call template

  • Invoking a template is a three-step process
  1. Find a template
  2. Defining context
  3. Apply colours to a drawing template
  • Open the booktst/views.py file and invoke the template file defined above
#coding:utf-8 from django.http import HttpResponse from django.template import loader,RequestContext def index(request): # 1. Template =loader.get_template('booktest/index.html') # 2 Context =RequestContext(Request,{'title':' book list','list':range(10)}) # 3 Return HttpResponse(template.render(context))Copy the code
  • Open the browser to refresh the new page, the effect appears as follows

View call template shorthand

  • The view-calling template does all three, so Django provides a function called render that encapsulates the above code
  • The render method takes three arguments
  1. The first argument is the Request object
  2. The second parameter is the template file path
  3. The third argument, a dictionary, represents the context data passed into the template
  • Open the booktst/views.py file and call render as follows
#coding:utf-8 from django.shortcuts import render def index(request): Context ={'title':' booklist ','list':range(10)} return render(request,'booktest/index.html',context)Copy the code

End of the project

Defining a view

  • Write the booktest/views.py file as follows
From Django. shortcuts import render from models import BookInfo def index(reqeust): Booklist = bookinfo.objects.all () Return render(reqeust, 'booktest/index.html', {'booklist': Def detail(reqeust, id) {def detail(reqeust, id); Objects.get (pk=id) Return render(reqeust, 'booktest/detail.html', {'book': book})Copy the code

Define the URLconf

  • Write the booktest/urls.py file as follows
From django.conf.urls import url # import views urlpatterns = [# configure the url(r'^$', views.index), {r'^(\d+)$',views.detail),}Copy the code

Define the template

  • Write templates/booktest/index. The HTML file is as follows
< HTML > <head> <title> </head> <body> <h1> </h1> <ul> {# for book in booklist%} <li> { And set up the hyperlink, link address is a number #} < a href = "{{book. Id}}" > {{book. Btitle}} < / a > < / li > {% endfor %} < / ul > < / body > < / HTML >Copy the code
  • Write templates/booktest/detail. HTML file is as follows
< HTML > < head > < title > detail page < / title > < / head > < body > {# output book titles #} < h1 > {{book. Btitle}} < / h1 > < ul > {# find books through relations of all hero, And traverse #} {% for hero in the book. Heroinfo_set. All %} {# # the output of the hero's name and description} < li > {{hero. Hname}} - {{hero. Hcontent}} < / li > {% endfor %} < / ul > </body> </html>Copy the code