Foreword
Under Django framework views, templates
Properties Methods views view function
There are two core classes at http: HttpResquest class and the class HttpResponse
requesr request.path: url to access the current path request.get_full_path (): full path of the url parameters request.method: returns the requested manner, i.e., GET or POST request.POST.get("") / GET.get("") : 得到value redirect redirect ( " .html " ): Jump html interface, you need to import redirect render render(request,".html",{key:value}) : 渲染templates the render (Request, " .html " , about locals ()): all variables passed to the function template render_to_response ( " .html " , {Key: value}): with the render (), parameters do not need to request, to import render_to_response
templates template syntax
Composition of the template: html codes + logic control statements
Variable {{var_name}} {{Var_name}}: two braces referring to a variable var_name {{. . 1 }}: form the target variable with the display list. {{Var_name.key}}: a variable display .key dictionary form, the class attribute may also be Filter obj {{ | filter: param}} var_name {{ | First}}: taking a first string var_name {{ | length}}: Take the string length var_name {{ | Slice: -1 }}: Slice var_name {{ | urlencode}}: for encoding url var_name {{ | the Add: 50 }}: adding a value corresponding to the variable var_name {{ | addslashes}}: value prior to adding the corresponding variable quotes {var_name { | capfirst}}: capitalize the first letter {var_name { | Cut: " " }}: Remove the specified characters var_name {{ | DATE: " Ymd " }}: custom time format (Y years, m months, d day) {var_name { | default: " " }}: If the variable is not empty, return to the default value Name {{ | default_if_none: " " }}: If the variable is the default value none, returns {var_name { | safe}}: If you pass into html language, and do not need to render, we shall add safe Tag (Tag) { %} {Tags%%% endtags } { % URL '' % }: reference address routing configuration { % OFF autoescape%} {%}% endautoescape: turn off security mechanisms, rendering direct html statement, with var_name {{| Safe}} { % IF %} {% elif %} {% the else %% endif} {% }: Analyzing { % For value in var_name%} {%} {empty%%% endfor }: traversal, value is the value {{Forloop.counter}} displays the index, a default start, {{Forloop.counter0} default start 0 {{Forloop.revcounter}} countdown {{Forloop.frist}} is true when the first pass { % Empty% }: If empty proceeds { % Csrf_token% }: POST submit data security, add key { %}% With simpename = {% endwith The longname% }: instead of the simple names of the complex name { % Verbatim%} {% endverbatim% }:禁止render { %}% Load: load a label library
Custom filter and simple_tag
a, create templatetags module in the app
b, create any .py files, such as: my_tags.py
from django import template from django.utils.safestring import mark_safe Register = template.Library () # Register name is fixed @ register.filter # decorator DEF fliter_multi (X, Y): return Y @register.simple_tag def simple_tag_multi(x,y): return x*y
c, introduced in gtml my_tags.py files and customized filter in simple_tag: {% load my_tags.py%} top row write
d, and use the filter simple_tag
{% load myTag %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> Pass parameters {# #} could send a {{ var_name|fliter_multi:l }} {# Can not be used in the control statement #} {% simple_tag_multi 3 5 %} </body> </html>
e, in INSTALLED_APPS configuration settings in the current app, django otherwise unable to find simple_tag custom
Inherited label extend and add tags include
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> < Div > Parent interface </ div > <div> {% block content %} {#} # This is a sub-interface {% endblock %} </div> </body> </html>
{% extends "father.html" %}
{# Inherit the parent interface for all content #}
{% block content %}
{# #} Content sub-interface
{% endblock %}