Some review of Django (mainly operations on the database)

Source: Internet
Author: User

Life cycle of 1.diango requests
Url-> Routing System--view function (get template + data) rendered--string returned to user
2. Routing system
/index/---> Functions or Classes. As_view ()
/detail/(\d+)---Parameters of the > function or the parameters of the class
/detail/(? p<nid>\d+)---The parameters of the > function or the parameters of the class----specify parameters
/detail/----> include ("App01.urls") route distribution.
3. View functions
FBV: Functions
def index (Request,*args,**kwargs)
..
CBV: Class
Class Home (view. View)
def get (Self,request,*args,**kwargs)
...

To get the data requested by the user:
Request. Post.get () (POST request)
Request. Get.get () (GET request)
Request. Files.get () (for transferring files)
... getlist ()
Request.path_info ()

File Object = Request. Files.get ()
File object. Name
File object. Size
The file object. Chunks ()

To return data to the user:
Render (request, mainly the path to the local HTML template file [if there is a rendering, the returned data is given back. ])
Redirect (request, mostly return URL)
HttpResponse (String)

4. Template language
: Render (request, mainly the path to the local HTML template file [if there is a rendering, the returned data is given back.) ])

<body>
{%for I in k2%}
{{i}}
{% ENFOR%}
{%for i in k2.keys%}
{{i}}
{% ENFOR%}
{%for i in k2.values%}
{{i}}
{% ENFOR%}
{%for k,v in k2.items%}
{{k}}-{{v}}
{% ENFOR%}
</body>



4.ORM
A. Creating Classes and fields
Class User (models. Model):
id = models. Intergerfiled ()
Name = models. CHARFIENLD (max_length =) #字符长度

Python manage.py makemigrations
Python manage.py Migrate
#settings. PY Registration App

B. Operation

Increase
Models. User.objects.create (name = "xxxx", age = ' xxxx ')
DIC = {' name ': ' xxx ', ' age ': ' 19 '}
Models. User.objecs.create (**dic)

obj = models. User (name = "xxxx", age = ' xxxx ')
Obj.save ()


By deleting
Models. User.objecs.filter (XXX). Delete ()
Change
Models. User.objecs.filter (Id__gt =1). Update (name = ' jxhs ') #把数据库中id大于1的中id大于1的 change the name value to JXHS
DIC = {' name ': ' xxx ', ' age ': ' 19 '}
Models. User.objecs.filter (id__gt = 1). Update (**dic) #用字典格式上传
Check
Models. User.objecs.filter (id = 1) #等于
Models. User.objecs.filter (id_gt = 1) #大于
Models. User.objecs.filter (id_lt = 1) #小于
Models. User.objecs.filter (Id_gte = 1) #大于等于
Models. User.objecs.filter (Id_lte = 1) #等于小于


Federated queries
Models. User.objecs.filter (id = 1, name = ' root ')
DIC = {' name ': ' xxx ', ' age ': ' 19 '}
Models. User.objecs.filter (**dic)

A fuzzy query for a federated query of a dictionary

DIC = {' name ': ' xxxx ' age_gt: ' + '} #名字为xxxx and older than 19
Models. User.objecs.filter (**dic)


Some review of Django (mainly operations on the database)

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.