current position:Home>Application of Django template

Application of Django template

2022-05-15 05:30:02Game programming

1、django Template setting and simple application

A template is a text , be used for Separate the presentation form and content of the document . Separating templates from views

1)、 stay bigdata02 Create... Under the directory templates Directory and create index.html file , The entire directory structure is as follows

Be careful : The template folder must be named templates

Index.html File code

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><!--{{hello}} Parameter use 2 individual {} Cover up --><!-- The parameter is the dictionary --><h1> today {{hello}} It's raining </h1><!-- The parameter is the list --><h1> today {{world}} It's raining </h1><!-- The argument is a string --><h1> today {{us}} It's raining </h1><h1> ha-ha     <!--     From capital to lowercase -->    <p>{{ggboy | lower}}</p>    <!--     Get the first letter of the word -->    <p>{{ggboy | first}}</p>    <!--     Get the first letter of the word , And capital letters to lowercase -->    <p>{{ggboy | first | lower}}</p>    <!--     Get the first letter of the word , And turn lowercase letters to uppercase -->    <p>{{us | first | upper}}</p></h1><!-- Get the first two characters of the word --><h1> today {{ggboy | truncatechars:3}} It's raining </h1><!-- Show variable  bio  Before  2  Word .--><h1> today {{world | truncatewords:2}} It's raining </h1><!--addslashes :  Add a backslash to any backslash 、 Before single or double quotation marks . stay world The number of variables 4 It's worth ( contain \) Before to add \ --><h1>{{world.4 | addslashes}}</h1><!-- Time filter ,date :  Format string parameters as specified  date  perhaps  datetime  object , Format  Y-m-d hello }}H:i:s  return   year - Month day   Hours : minute : second   Format time .--><h1>{{hello.time | date:"Y-m-d"}}</h1><!-- Get the length of the variable --><h1>{{hello | length}}</h1><!--filesizeformat: Display the size of the file in a more readable way ( namely '13 KB', '4.1 MB', '102 bytes' etc. ). Dictionary returned --><!-- Is the number of key value pairs , The set returns the length after de duplication .--><h1>{{hello.num | filesizeformat}}</h1><!--default  Provide a default value for the variable .--><!-- If  hello.w  The Boolean value of the passed variable is  false, The specified default value is used nothing.--><h1>{{hello.w | default:"nothing"}}</h1><!--  Mark the string as safe , There is no need to escape .--><!-- Mark the string as safe , There is no need to escape .--><h1>{{ | safe}}</h1><h1> It will be fine tomorrow </h1><!-- if/else  Tag syntax format --><ul>    {% if|length > 4 %}        {{hello.zansan}}    {% else %}        {{hello.lisi}}    {% endif %}</ul><!--for  label --><ul><!--     here hello Equivalent to hello.key-->    {% for i in hello%}        {{i}}    {% endfor %}</ul><ul>    {% for i in hello.values%}        {{i}}    {% endfor %}</ul></body></html>
Django Application of template - The first 1 Zhang

2) From the template, we know that the variable uses double brackets .

towards Django Describe the path of the template file , modify mysite/
modify TEMPLATES Of DIRS by [os.path.join(BASE_DIR, 'templates')] among BASE_DIR by bigdata02 The root path 'templates' Name the template folder

3) Now modify, Add a new object , Used to submit data to a template :

from django.shortcuts import render # return html file from datetime import datetimedef index(request):    context = {}# Parameters passed in from the view , Using a dictionary     #  The value of the parameter can be a string 、 Dictionaries 、 list 、 aggregate     # context['hello'] = 'python',# The key of the dictionary must be the same as templates Under the folder html The parameters in the file are consistent     context['ggboy'] = 'PYTHON'    context['us'] = 'kfc'    context['world'] = ['django', 'flask', 'scrapy','\hq']    context['hello'] = {'zansan':'male', 'lisi': 'female', "time":,                        'time2': False,"num": 2048, 'w': '',                        'link': "<a href=''> Click the jump </a>"}    return render(request,"index.html", context) # Return to rendering html page , Pass in context to html In file 
Django Application of template - The first 2 Zhang

4) Add a new route in the route

from django.contrib import adminfrom django.urls import path,re_pathfrom .views import indexurlpatterns = [    path('admin/',,    re_path("^index/$", index) # With index start ,/ The ending matches ]
Django Application of template - The first 3 Zhang

Effect demonstration

Django Application of template - The first 4 Zhang
Django Application of template - The first 5 Zhang

author :857weir09432

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .

copyright notice
author[Game programming],Please bring the original link to reprint, thank you.

Random recommended