程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django template

編輯:Python

DTL

DTL(django template language) yes Django The template language comes with .

  • Template variable
    { { Variable }} Use double braces to include , Notice that there are spaces on the left and right
  • Template tags
    {% %} Template labels include , Write some statements .
label effect {% if %} {% endif %} {% elif %}if sentence {% for %} {% endfor %} Loop statement {% url %} Address {% extends xx %} Template inherits tags , from xx In the inheritance {% load %} Load related content {% static %} Static resources {% block %} {% endblock %} Space occupying tags {% csrf_token %} Used to prevent cross site forgery attacks {% include %} Contains a HTML page

for There are some common variables in the loop

  1. forloop.counter Current index , from 1 Start
  2. forloop.counter() Current index , from 0 Start
  3. forloop.revcounter The number of cycles remaining
  4. forloop.revcounter() The number of cycles remaining -1
  5. forloop.first Whether the first cycle
  6. forloop.last Whether the last cycle
  7. forloop.parentloop Nested loop , Get the cycle of Volume 1
  • filter

{ { Variable | filter : Parameters }}

Templates effect safe Turn off label auto escape length Get the length of the variable default When variables are False, Show default date Date format upper/lower Case write slice section { { name

Example

app1 Under the views.py

def var(request):
lists = ['java', 'Python', "c"]
dicts = {

' full name ': " Ash ",
" Age ": 24
}
return render(request, 'app1/var.html', {
'lists': lists, 'dicts': dicts})

var.html

{
{ dicts. full name }}
<br />
{% for list in lists %}
<p>{
{ list }}</p>
{% endfor %}

Customize

In the corresponding app1 Under the new templatetags Under the document , Then build __init__.py Files and self created filters .

Write... In the corresponding filter , Here, if the corresponding value is greater than the passed in parameter , Then divide and add … To display

from django import template
register = template.Library()
@register.filter
def show_title(value, n):
if len(value) > n:
return f'{
value[0:n]}...'
else:
return value

The corresponding template file

{% load myfilter %} {
{ dicts. title |show_title:10 }}

Custom labels are consistent with the above principles .

Template inheritance

Create a new one base.html Page as template

<html>
<head></head>
{% block title %}
<title> This is the master </title>
{% endblock %}
<body>
<table border="1" >
<tr>
<td colspan="2" >
This is a top Area , Generally used for navigation
</td>
</tr>
<tr >
<td > This is the menu on the left </td>
<td >
{% block content %} This area changes as the content page changes {% endblock %}
</td>
</tr>
<tr>
<td colspan="2" >
This is the bottom area , Generally used for navigation
</td>
</tr>
</table>
</body>
</html>

Create a new page , Inherit base page .

{% extends "app1/base.html" %} {% block title %}
<title> This is the welcome page </title>
{% endblock %} {% block content %}
<div > Welcome to my special shop </div>
{% endblock %}

New route , test , You can also use {% include %}


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved