In Django template T--

Templates Templates

Templates folder

Templates folder to store the template file, you can use the template syntax, pay attention to static html files inside the folder can not use the template syntax .

Templates can be customized name, applications need to be registered in the sub-directory, you need the project directory in the settings.pyregistration, and then the folder labeled Template Folter.

Django's default template configuration

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    }
]

In return inside views to be rendered in return, by render()wayreturn render(request, 'Template.html', locals())

Template syntax

Structure tags

  • block
    • Definition of a block, the subclass tag reference.
  • extends
    • Html file to load the parent class{% extends 'base.html' %}
  • include
    • Try not to use

Use block tag to load static files:

{% load static %}<!--不可省略-->
<!--使用block把html代码分割,extCSS是自定义命名-->
{% block extCSS %}
    {{ block.super }}<!--继承父类block的内容-->
{% endblock %}

Use block + extends, zero for the whole, include from zero of a

Parent tag define a block base.html

{% block header %}
<h3>
        this is a header block
</h3>
{% endblock %}

Home.html in succession in the sub-tag

{% extends 'base.html' %}
{% block header %}
{# 这是一个模板注释,网页中检查元素是不可见的 #}
{% 这是一个多行模板注释,也是不可见的。默认子标签的内容会覆盖付标签的内容,通过super来继承福标签内容 %}
	{{ block.supper }}
<h3>
    this is a child html
</h3>
{% endblock %}

Variable syntax

Dot syntax to use acquisition variable, or return json dictionary data in the view, is acquired by the key tag.

<p>{{ params }}</p>
<p>{{ param.name }}</p>
<!-- 获取列表里面的单个元素,这里是一个用户对象 -->
<p>{{ user_list.1 }}</p>
<!-- 获取对象的属性 -->
<p>{{ user_list.2.name }}</p>
<!-- 可以在试图函数里面使用__str__将对象转化为字符串 -->

Using dot syntax to refer to object methods:

<p>biger:{{ dic.name.upper }}</p>

Tag syntax

Shaped like a {% tag %}statement that Django tag syntax, you can implement complex logic, some of the labels need to start and end tags

  1. for loop
<h3>循环取值1</h3><hr>
{% for item in person_list %}
    <p>{{ item.name }},{{ item.age }}</p>
<!-- 一个可选的empty标签,如果没有元素是展示 -->
{% empty %}
    <p>sorry,no person here</p>
{% endfor %}

<h3>循环取值2:倒序</h3><hr>
{% for item in person_list reversed %}
    <!--序号从1开始-->
    <p>{{ forloop.counter }}----->{{ item.name }},{{ item.age }}</p>
    <!--序号从0开始-->
	<p>{{ forloop.counter0 }}----->{{ item.name }},{{ item.age }}</p>
	<!-- 序号倒序 -->
	<p>{{ forloop.revcounter }}----->{{ item.name }},{{ item.age }}</p>
{% endfor %}

<h3>循环取值3:字典</h3><hr>
{% for k,v in d.items %}
    <p>{{ k }},{{ v}}</p>
{% endfor %}
  1. if the label
{% if i > 300 %}
    <p>大于{{ i }}</p>
{% elif i == 200  %}
    <p>等于{{ i }}</p>
{% else %}
    <p>小于{{ i }}</p>
{% endif %}
  1. with label

Use a simple name cache a complex variable, when you need to use an "expensive" method (such as database access) many times when it is very useful

{% with total=business.employees.count %}
	<!-- 复数显示及国际化使用pluralize -->
    {{ total }} employee{{ total|pluralize }}
{% endwith %}
  1. csrf_token

CSRF marker for protection

{% csrf_token %}
  1. Escape switch

Role: views.py pass over the dictionary value is a string of html code, default is based on the output string, if the escape will be compiled into the output html page format

  • safe method of escape
{ contents|safe }<!--添加safe完成转义,不建议-->
  • Escape wrapped up
<!--off是关闭转义,on是开启-->
{% autoescape off %}
  {{ content }}  
{% endautoescape %}

Render Template

Template rendering return, you can use the render method can also be used HttpResponse to achieve render function

# 首先加载模板文件
# 返回<class 'django.template.backends.django.Template'>
template = loader.get_template('Hello.html')
# 渲染模板
result = template.render(context={"msg": "okba"})
# 用HttpResponse返回
return HttpResponse(result)

Guess you like

Origin blog.csdn.net/qq_27114273/article/details/92397609