Routing system:
1. DEF index (request)/index/
2.
/detail-(\d+)/def detail (Request,nid)
/detail-(? p<nid>\d+)/def detail (Request,nid)
3.
/index/, DEF index (request) NAME=N1
To generate a URL using an alias:
Template language: {% URL n1%},/index/
View functions:
Reverse (name= "N1"),/index/
4.
/web/include ("App01.urls")
View functions:
1. Function at least one parameter
2. Request
Request.method
Request. GET
Request. POST (Request header: content-type:application/x-www-form-urlencoded; charset=utf-8)
Request.body
Request. FILES
...
3.
return HttpResponse (..)
return render ()
Return Redirect ()
Template engine:
1. Basic syntax
return Renderi (Request, ' xxx.html ', {' V ': [1,2,3,4], ' d ': {' K1 ': ' v1 ', ' K2 ': ' V2 '}})
Xxx.html
{{V.2}}
{% for I in d%}
{{i}}--Key
{% ENDFOR%}
{% for k,v in d.items%}
{{K}}--{{v}}
{% ENDFOR%}
2. Functions
Django provides functions
Simple_tag
Filter
ORM Operation:
1. Create a table
Class UserInfo (models. Model):
# nid = Models. Autofield (primary_key=true) int
# nid = Models. Bigautofield (primary_key=true) long
Name = models. Charfield (MAX_LENGTH=32)
PWD = models. Charfield (MAX_LENGTH=32)
2. Operation table
Q = models. UserInfo.objects.all ()
Queryset = [obj (id,name,pwd), obj (id,name,pwd), obj (id,name,pwd),]
Q = models. UserInfo.objects.values (' name ', ' pwd ')
Queryset = [{' Name ': ' Alex ', ' pwd ': 123},{' name ': ' Alex1 ', ' pwd ': 123sfd},{' name ': ' Alex1 ', ' pwd ': 123SFD},]
Q = models. UserInfo.objects.values_list (' name ', ' pwd ')
Queryset = [(' Alex ', 123), (' Alex ', 123), (' Alex ', 123), (' Alex ', 123),]
Q = models. UserInfo.objects.filter (name= ' Alex ')
[Obj,]
Q = models. UserInfo.objects.get (name= ' Alex ')
Q = models. UserInfo.objects.filter (name= ' Alex '). First ()
...
Today's content:
1. FBV and CBV
FBV:
URL (r ' ^index/', Views.index),
def index (Request):
Print (' ... ')
if Request.method = = ' GET ':
Pass
elif Request.method = = ' POST ':
Pass
return HttpResponse (' .... ')
CBV:
URL (r ' ^user/', views. User.as_view ()),
Class User (View):
Def dispatch (self, request, *args, **kwargs):
Print (' before ')
obj = Super (user,self). Dispatch (Request, *args, **kwargs)
Print (' after ')
return obj
def get (self,request):
Print ("Get ...")
return HttpResponse (' ... ')
Def post (self,request):
Print ("Post ...")
return HttpResponse (' ... ')
2. ORM Operation
A. Creating a table
One-to-many
Many-to-many
-Create a third table:
-Define your own third table: Columns Unlimited
-Manytomanyfield field: Column limit (iii)
-cannot be directly, can only be indirectly manipulated through the Manytomanyfield field
B. Operation
Forward to
Dp
Reverse
UserInfo
Userinfo_set
3. Cookies
Saved on the browser is absolutely right
Application:
Can do user login
Make a vote
4. Session
Server-side saving is absolutely right
{
ASDFASDFASDF: {' user ': ' asdf ', ' PWS ': ' ASDF '}
}
5. Ajax Operations
#
$.ajax ({
URL: '/aj/', # Submit address
Type: "POST", # Submission method
Data: {uuu:u, ppp:p}, # Submit
DataType: "JSON",
Success:function (data) {# callback function, executed automatically after successful login
# A dictionary-form string that is serialized as a Dictionary object (JSON object)
# var data_dict = json.parse (data);
if (data_dict.status) {
Location.href = "/home/"
}else{
alert (data_dict.error);
}
}
})
Djange table Operations and Ajax