Python 107th days -- Django basics 2, zero seven days django

Source: Internet
Author: User

Python 107th days -- Django basics 2, zero seven days django

1. Django request Lifecycle

Routing System-> attempted function (get template + Data => rendering)-> string returned to the user

2. Routing System

/Index/-> function or class. as_view ()
/Detail/(\ d +)-> function (parameter) or class. as_view () (parameter)
/Detail /(? P <nid> \ d +)-> function (parameter) or class. as_view () (parameter)
/Detail/-> include ("app01.urls ")
/Detail/name = 'a1'-> include ("app01.urls ")
-View: reverse
-Template: {% url "a1" %}

3. View
Chen Yibo:
FBV: Function
Def index (request, * args, ** kwargs ):
..

CBV: Class
Class Home (views. View ):

Def get (self, reqeust, * args, ** kwargs ):
..

Obtain the data in the user request:
Request. POST. get
Request. GET. get
Reqeust. FILES. get ()

# Checkbox,
... Getlist ()

Request. path_info


File object = reqeust. FILES. get ()
File object. name
File object. size
File object. chunks ()

# <Form special Settings> </form>


Return data to the user:
Render (request, "template file path", {'k1': [,], "k2": {'name': 'xxx', 'age ': 73 }})
Redirect ("URL ")
HttpResponse (string)


4. template Language
Render (request, "template file path", {'obj ': 1234, 'k1': [,], "k2": {'name ': 'sss', 'age': 73 }})
<Html> <body> 

 



5. ORM

A. Create classes and fields
Class User (models. model): age = models. intergerFiled () # The integer does not need to be added with the length name = models. charField (max_length = 10) # character length Python manage. py makemigrations python manage. py migrate # settings. py register APP

 



B. Operations

Add
Models. user. objects. create (name = 'qianxiaohu ', age = 18) # method 1 dic = {'name': 'XX', 'age': 19} # method 2 models. user. objects. create (** dic) obj = models. user (name = 'qianxiaohu ', age = 18) # method 3 obj. save ()

Delete
 models.User.objects.filter(id=1).delete()

 


Change
models.User.objects.filter(id__gt=1).update(name='alex',age=84)dic = {'name': 'xx', 'age': 19}models.User.objects.filter(id__gt=1).update(**dic)

 


Query
Models. user. objects. filter (id = 1, name = 'root') models. user. objects. filter (id _ gt = 1, name = 'root') # greater than models. user. objects. filter (id _ lt = 1) # smaller than models. user. objects. filter (id _ gte = 1) # greater than or equal to models. user. objects. filter (id _ lte = 1) # smaller than or equal to models. user. objects. filter (id = 1, name = 'root') # directly write dic = {'name': 'XX', 'Age _ gt ': 19} # define the dictionary models. user. objects. filter (** dic) v1 = models. business. objects. all () # QuerySet, internal elements are all objects # QuerySet, internal elements are all dictionaries v2 = models. business. objects. all (). values ('id', 'caption ') # QuerySet, internal elements are tuples v3 = models. business. objects. all (). values_list ('id', 'caption ') # obtain an object. If it does not exist, models is returned. business. objects. get (id = 1) object or None = models. business. objects. filter (id = 1 ). first ()

 




Foreign key:
V = models. Host. objects. filter (nid _ gt = 0)
V [0]. B. caption ----> Cross-table through.

Filter (B _ caption) # Use _ double underscores to query cross-Table Conditions




Foreign key:
Example:
Class UserType (models. model): caption = models. charField (max_length = 32) id caption #1, regular User #2, VIP User #3, tourist class User (models. model): age = models. intergerFiled () name = models. charField (max_length = 10) # character length # user_type_id = models. intergerFiled () # constraint, user_type = models. foreignKey ("UserType", to_field = 'id') # constraints, foreign key name age user_type_id # user 1 18 3 # represents user 1 as a tourist # user 2 18 2 # represents user 2 as a VIP user # user 3 18 2

Window relative positioning open relative positioning use parent relative positioning use start show absolute positioning
Position: fixed relative absolute




Ajax

$. Ajax ({
Url: '/host ',
Type: "POST ",
Data: {'k1': 123, 'k2': "root "},
Success: function (data ){
// Data is the string returned by the server.
Var obj = JSON. parse (data );
}
})
$. Get (url = 'XX', data = 'xxx ')
$. GetJson
$. Post


Suggestion: always let the server return a dictionary

Return HttpResponse (json. dumps (dictionary ))




Many to many:
Create multiple-to-multiple:
Method 1: Custom relational table class Host (models. model): nid = models. autoField (primary_key = True) hostname = models. charField (max_length = 32, db_index = True) ip = models. genericIPAddressField (protocol = "ipv4", db_index = True) port = models. integerField () B = models. foreignKey (to = "Business", to_field = 'id') #10 class Application (models. model): name = models. charField (max_length = 32) #2 class HostToApp (models. model): hobj = models. foreignKey (to = 'host', to_field = 'nid') aobj = models. foreignKey (to = 'application', to_field = 'id') operation associated tables are the same as other tables # HostToApp. objects. create (hobj_id = 1, aobj_id = 2) Method 2: automatically create a relational table class Host (models. model): nid = models. autoField (primary_key = True) hostname = models. charField (max_length = 32, db_index = True) ip = models. genericIPAddressField (protocol = "ipv4", db_index = True) port = models. integerField () B = models. foreignKey (to = "Business", to_field = 'id') #10 class Application (models. model): name = models. charField (max_length = 32) r = models. manyToManyField ("Host") # automatic association. You cannot directly operate on the third table (obj = Application) after creating or creating the table. objects. get (id = 1) # get a record object obj. name # operate obj in the third table. r. add (1) # The object corresponding to this record. r. add (2) obj. r. add (2, 3, 4) obj. r. add (* [1, 2, 4]) obj. r. remove (1) obj. r. remove (2, 4) obj. r. remove (* [1, 2, 3]) obj. r. clear () # clear all the Mappings of the current object obj. r. set ([3, 5, 7]) # modify the ing relationship of the current object # all related host objects "list" QuerySet obj. r. all () # obtain all Mappings of the current object

 




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.