Chapter one django Installation and introduction
Chapter two django Based on using
The third chapter The routing layer
Chapter four A virtual environment 、django Version difference 、 View layer
The fifth chapter Formwork layer
Chapter six The model layer
html notes :
html Its own comments can be seen in the browser developer tool
<!-- The comment -->
django Template syntax notes :
django The comments of template syntax will not be displayed in the browser developer tool
{# The comment #}
{ { Data objects | Filter name : Parameters }} The filter can only transmit one additional parameter at most
django Template syntax provides 60 Multiple filters
<p> The length of the statistics :{
{ s1|length }}</p>
<p> Arithmetic addition or string addition :{
{ n1|add:111 }}、{
{ s1|add:'big baby' }}</p>
<p> Convert the number to the appropriate document unit of measurement :{
{ file_size|filesizeformat }}、{
{ file_size1|filesizeformat }}</p>
{# If b The value is false Will show : Later content #}
<p> Judge whether the Boolean value corresponding to the current data object is False:{
{ b|default:' The Boolean value corresponding to the previous value is False' }}、{
{ s1|default:' The Boolean value corresponding to the previous value is False' }}</p>
{# If b The value is none Will show : Later content #}
<p> Judge whether the Boolean value corresponding to the current data object is False:{
{ b|default_if_none:' The Boolean value corresponding to the previous value is None' }}、{
{ s1|default_if_none:' The Boolean value corresponding to the previous value is None' }}</p>
{# ctime Value is converted to date Time format after Corresponding year - month - Japan when - branch - second #}
<p> Time format :{
{ ctime|date:'Y-m-d H-i-s' }}</p>
<p> Index slice :{
{ s1|slice:'0:8' }}</p>
<p> Intercept the specified number of text according to the space :{
{ s2|truncatewords:5 }}、{
{ s1|truncatewords:1 }}</p>
<p> Intercept text according to the number of characters ( Contains three points ):{
{ s2|truncatechars:5 }}、{
{ s1|truncatechars:10 }}</p>
<p> Remove the specified character :{
{ info|cut:'|' }}</p>
{# safe You can match the background parameters with html The syntax is transformed into pages #}
<p> Whether to cancel the conversion :{
{ tag1 }}、{
{ tag1|safe }}、{
{ scripts1|safe }}</p>
{# You can also handle it first when the view function passes values , In this way, it can also be directly escaped into a page #}
from django.utils.safestring import mark_safe
ttt = '<a href="https://www.baidu.com"> Am I </a>'
res = mark_safe(ttt)
<p>{
{ res }}</p>
{% name ...%}
{% end name %}
{% if Conditions 1 %}
<p> How do you do </p>
{% elif Conditions 2 %}
<p> How is he </p>
{% else %}
<p> Hello everyone </p>
{% endif %}
{% for i in l1 %}
<p>{
{ i }}</p>
{% endfor %}
stay {% for %} Internal loop , You can visit a website called forloop Template variables for . This variable has several properties , They give you some information about the loop process .
{‘parentloop’: {}, ‘counter0’: 0, ‘counter’: 1, ‘revcounter’: 4, ‘revcounter0’: 3, ‘first’: True, ‘last’: False}
for+if Other uses
{% for i in l1 %}
{% if forloop.first %} {# forloop.first value #}
<p> This is the first cycle </p>
{% elif forloop.last %}
<p> This is the last cycle </p>
{% else %}
<p> Intermediate cycle </p>
{% endif %}
{% empty %}
<p>for The loop object is empty Automatic execution </p>
{% endfor %}
1. Under the application, you need to create a file named templatetags Folder
2. Create a folder with any name in this folder py file
3. In the py You need to write two lines of fixed code in advance
from django import template
register = template.Library()
templatetags Self construction under the environment py file
@register.filter(is_safe=True)
def index(a, b):
return a + b
Custom filter : Only two parameters can be received
{% load self-built py File name %}
{
{ n1|index:666 }}
templatetags Self construction under the environment py file
@register.simple_tag(name='my_tag')
def func1(a, b, c, d):
return a + b + c + d
{% load self-built py File name %}
{% my_tag 1 2 3 4 %} # The parameters can be separated by spaces
inclusion_tag Is a partial page , Create local pages that need to be used repeatedly as inclusion_tag It can reduce code redundancy , At the same time, it makes the code more readable
First create a templatetags Folder
Put an arbitrary name in it py The corresponding contents are as follows :
# mytag.py
@register.inclusion_tag(' Template style file ') # Register the corresponding tag, have access to name Specify the name of the call without specifying that the function name should prevail
def func(n):
l = []
for i in range(1, n + 1):
l.append(f' The first {
i} page ')
return locals()
Set template style file
<ul>
{% for foo in l %}
<li>{
{ foo }}</li>
{% endfor %}
</ul>
Insert the corresponding customization where you need to use tag
{% load Customize tag file name %}
{% Function name or name Name Then pass on the reference ( If any ) %}
<!-- Example code -->
{% load mytag %}
{% func 10 %}
It's similar to going to html The partial pages on the page are made in the form of modules Where you want to import directly, you can display
Usage mode
{% include 'menu.html' %}
Object oriented inheritance : If you inherit a page, you can use all the resources on that page
There should be at least three areas on the template :
css Area 、 Content area 、js Area
After delimiting three areas, the sub page can have its own independent css、js、 Content
1. First in the template through block Delimit areas that can be modified in the future
{% block content %}
<h1> Home page content </h1>
{% endblock %}
2. The child board inherits the template
{% extends 'home.html' %}
3. Modify the designated area
{% block content %}
<h1> Login content </h1>
{% endblock %}
4. Child pages can also reuse the content of the parent page
{
{ block.super }}