Django template, djangotemplate
I. Introduction
Add the template language (including the variables and tags that will be replaced) on the basis of HTML)
Ii. template syntax variables ({{}})
2.1 basic usage
Views:
Def index (request): import datetime s = "hello" l = [111,222,333] # list dic = {"name": "yuan", "age ": 18} # dictionary date = datetime. date (1993, 5, 2) # date object class Person (object): def _ init _ (self, name): self. name = name person_yuan = Person ("yuan") # custom Class Object person_egon = Person ("egon") person_alex = Person ("alex") person_list = [person_yuan, person_egon, person_alex] return render (request, "index.html", {"l": l, "dic": dic, "date": date, "person_list": person_list })
Template:
<H4 >{{ s }}
2.2 Filter
Syntax:
{Variable | filter: parameter }}
# Addition {I | add: 5 }}# if a variable is false or empty, use the given default value. Otherwise, use the variable value {value | default: "nothing" }}# return value length. It applies to strings and lists {value | length} # format the value into a "human-readable" file size (for example, '13 kb', '4. 1 Mbit/s, '100 bytes ', etc.) {value | filesizeformat }# if value = datetime. datetime. now () {value | date: "Y-m-d" }}# slice {value | slice: "2:-1" }}# truncation string. The truncated string ends with a translated ellipsis. {Value | truncatechars: 9} # truncation word. The truncated word will be translated into a ellipsis ("... ") at the end of {value | truncatewords: 2} ''' the Django template automatically escapes HTML tags, JS and other syntax tags for security reasons. However, sometimes we may not want these HTML elements to be escaped. For example, if we create a content management system, the post added in the background is modified, these modifiers may be text that is similar to FCKeditor editing with HTML modifiers added. If they are automatically escaped, the source file that protects the HTML Tag is displayed. There are two methods to disable the automatic conversion of HTML in Django. If it is a separate variable, we can use the filter "| safe" to tell Django that this code is safe and does not need to be escaped. ''' Value = "<a href =" "> click </a>" {value | safe }}
Iii. Tag of template syntax ({%% })
3.1 tag operations
# For Loop {% for person in person_list %} <p >{{ person. name }}</p >{% endfor % }{% for key, val in dic. items %} <p >{{ key }}:{{ val }}</p >{% endfor % }#... empty {% for person in person_list %} <p >{{ person. name }}</p >{% empty %} <p> sorry, no person here </p> {% endfor %}
# If {% if num> 100 or num <0%} <p> invalid </p> {% elif num> 80 and num <100%} <p> excellent </p> {% else %} <p> join live </p> {% endif %}
#url{{% url ... %}}
3.2 template inheritance
Template inheritance allows you to create a basic "skeleton" template that contains all the elements on your site and defines blocks that can be overwritten by the quilt template.
Create a motherboard first
<!DOCTYPE html>
Use the html of the motherboard:
{% extends "base.html" %} {% block title %}My amazing blog{% endblock %} {% block content %}{% for entry in blog_entries %}