Basic Configuration
First, create a Django program
To create a project Terminal command:
Project:django-admin startproject mysitecd mysitepython manage.py Startapp app01
1. Routing system
General relationship:/index/---- functions ... #其他框架有 Reflection: /(\w+)/(\w+) /home/index/ = import: Home module, getattr (index) Fun () dynamic Relationship: /index/(\ w+) \ (parameter) routing distribution:/index/- app.url file [/index/(\w+) \-- functions (parameters,]
Each routing rule corresponds to a function in a view
URL (r ' ^detail/(\d+)/', views.detail), url (r ' ^detail2/(\d+)/(\d+)/', views.detail2), url (r ' ^detail3/(? p<p1>\d+)/(? p<x2>\d+)/', VIEWS.DETAIL3),
One-time classification according to the app routing rules
URL (r ' ^web/', include (' App01.urls ')),
2. Template
2. Template A. The process of template rendering B. {{K1}}--index yes. C. {% for item in k2%}<P>{{Item}},{{forloop.counter}},{{forloop.counter0}},{{forloop.first}},{{forloop.last}},{{Forloop.revcounter}}</P> {% endfor%} d. Built-in method of the template language E. Custom method filter Simple_tag 1, create the specified file, name cannot be changed Templat Etags 2, create any. py file, such as: xx.py from the Django Import template from Django.utils.safestr ing import mark_safe from django.template.base import resolve_variable, Node, Templatesyntaxerror # The Register = template must be the same. Library () # Create function @register. Filter def F1 (value): return value + "666" 3, in the HTML template header execution {% load xx%} 4, K1 = ' VVV ' {{K1}} = = VVV {{K1|F1}} = vvv666 5, settings.py Register App Summary: Filter Limitations: Parameter support: The IF condition of the template language Simple_tag not supported: The IF condition of the template language F. Template inheritance Motherboard: Balabala ... {% block name%} {% Endblock%} Balabala ... Sub-board: {% extends ' motherboard HTML filename '%} {% block name%} Content of specific sub-pages ... {% Endblock%} g. Include widget: x.html {% include ' x.html '%}View Code
Basic operation of Template language
views.py
def template (Request): return render (Request, ' template.html ', {' K1 ': ' VVV ', ' K2 ': [11,22,33,], ' K3 ': {' Nid ': ' Name ': ' Alex '}})
Template.html
{{K1}} {{k2.1}} Take the second of the list, indexed by. {{K3.name}}
Used in the For loop
{% for item in k2%} <p>{{Item}},{{forloop.counter}},{{forloop.counter0}},{{forloop.first}},{{forloop.last}}</p> { % endfor%}//Results 11,1,0,true,false22,2,1,false,false33,3,2,false,true
If
{% if K1 = = ' v1 '%}
3. Custom Simple_tag
A. Create the Templatetags module in the app
b, create any. py file, such as: xx.py
# First import from Django import templatefrom django.utils.safestring import mark_safefrom django.template.base import resolve_ Variable, Node, templatesyntaxerror# register this name must not be changed to register = template. Library () # Create function @register.filterdef F1 (value): return value + "666"
3.
Import the xx.py file name previously created in the HTML file that uses the custom Simple_tag
{% load xx%}
xx.py
from Import = template. Library () @register. Filterdef F3 (value): return"666"
Using the filter
{% if k1|f3%} Pass the K1 to the custom F3 Pass two parameters (up to two parameters can be passed)
@register. Filter def f3 (value,arg): return " 666 " + arg
Using the filter
{{k1|f3: "QL"}}
xx.py
@register. Simple_tagdef F1 (S1,S2,S3,S4): return s1 + s2 + s3 + S4
Using Simple_tag
{% F1 1 2 3 4}//1 represents the first parameter, and 2 represents the second parameter 、、、
4.
K1 = ' VVV ' {{k1}} = VVV {{K1|F1}} = VV
5.
Configure the current app in Settings, or Django can't find a custom Simple_tag
Summarize:
Filter
Limitations: Parameter Transfer
Support: If condition for template language
Simple_tag
Unsupported: If condition for the template language
Inheritance of templates
Master Board:
{% block name%} {% Endblock%}
Sub-board:
{% extends ' motherboard HTML filename '%} {% block name%} content of specific sub-pages ... {% Endblock%}
Include widget: x.html{% include ' x.html '%}
Ajax
function SubmitForm () { $.ajax ({ URL: '/web/ajax_demo/', type: ' POST ', data:{' user ': $ (' #username ') . Val (), ' pwd ': $ (' #pwd '). Val ()}, DataType: ' json ', success:function (data) { if (data.status) { Location.href = "http://www.baidu.com"; } else{ alert (data.message);}} ); }
Python Django Day18