(2) Django web development template

Time:2021-6-6

1. Template file

The template file consists of two parts

  • Static files: CSS, JS, HTML
  • Dynamic content: dynamically generate some web content, which needs to be realized through template language.

2. Use of template files

  • Load the template file, get the content of the template file, and generate a template object.
  • Define template context and transfer data to template file.
  • Template rendering generates HTML page content render, and replaces the corresponding variables with the transferred data to generate a standard HTML content after replacement.

2.1 common grammar

  • Variable related with {}}
  • Logic related with {%}

2.2 template variables

Template variable names are composed of numbers, letters, underscores, and dots. They cannot start with underscores.
Use template variable: {template variable name}},

2.3 analysis of template variables

When using template variables,. May be preceded by a dictionary, an object, or a list.
For example: {dogs. Name}}
1) Think of dogs as a dictionary and name as a key name
2) Treat dogs as an object and name as an attribute
3) The way to treat dogs as an object and name as an object
The templates are parsed in the order above.

2.4 template label

Syntax: {% code segment%}

  • For loop
{% for X in list%}
    ....
{% endfor %}
  • if
{% if ... %}
    ....
{% elif ... %}
    ....
{% endif  %}

Examples

            {% for i in books %}
                    <tr>
                        <td>{{ i.id}}</td>
                        <td>{{ i.title }}</td>
                        <td>{{ i.author  }}</td>
                    </tr>
             {% endfor %}
        <select>
        {% for i in books %}
            {% if i.id == 1 %}
                <option selected="selected" value="{{ i.id }}">{{ i.title }}</option>
            {% else %}
                <option value="{{ i.id }}">{{ i.title }}</option>
            {% endif %}
        {% endfor %}
        </select>

2.5 filter

In the template language of Django, by usingfilterTo change the display of variables.

Syntax: {{value | filter_ Name: parameter}

Django commonly used built-in filters

Reference documents:https://docs.djangoproject.co…

  • default
#If a variable is false or empty, the given default value is used 
{{ value|default:"hello"}}
  • filesizeformat
#Format the value as a human friendly readable file size (for example, '200 KB', '10.5 MB', '1.8 GB', etc.)
{{ value|filesizeformat }}
  • Format date
{{ value|date:"Y-m-d H:i:s"}}
  • Slice slice
{{value|slice:"1:-1"}}
  • safe

HTML tags in the template context are escaped by default, which can be turned off by using safe.

{{ value|safe}}

2.6 custom filter

A custom filter is just a python function with one or two arguments

Reference documents:https://docs.djangoproject.co…

Example:
1. Django project MySite, app, Application Book_ Show, customize other_ Filter.py in the following location

book_show/
    __init__.py
    models.py
    templatetags/
        __init__.py
        other_filter.py
    views.py

2.other_ Filter. Py, as follows

from django import template
register = template.Library()

@register.filter(name="cut")
def str_pre(value, pre_str):
    'add prefix'
    return "{}-{}".format(pre_str, value)

3. Use

First, import the custom filter file, and then it can be used normally
{% load other_filter %}