Summary of django knowledge points and django knowledge points

Source: Internet
Author: User

Summary of django knowledge points and django knowledge points

I. View functions:

Request object ----------- request:

1. HttpRequest. body: Request the original data

2. HttpRequest. path: A string indicating the requested path component (excluding the domain name)

3. HttpRequest. method

4. HttpRequest. GET

5. HttpRequest. POST

6. HttpRequest. FILES

7. HttpResquest. user: an object of the AUTH_USER_MODEL type.

Response object:

Return HttpResponse ("") returns the string instance

Return render (request, "template", {"": ""}) returns a string instance.

Return rredirect ("/index/") redirection

Ii. template Language

First, write the function in the views function and return a rendering page:

Views:

Def foo (request ):

Name = "li"

L = [3333,]

D = {"info": [obj1, obj2...]}

Return render (request, "index.html", locals ())

1. Variable: {name }}

------- Deep query period. For example: {d.info. 0. name }}

------- Filter {name | date: "Y-m-d "}}

2. Labels:

For Loop

{% For I in l %}

{I }}

{% Empty %}

<P> no conditions met </p>

{% Endfor %}

If judgment statement

{% If name = "li" %}

<P> Yes </p>

{% Elif %}

{% Endif... %}

3. template syntax

(1)in master edition base.html: {% block con %}{% Endblock %}

(2) inherit the master index.html:

{% Extends "base.html" %} inherits

{% Block con %}

{% Endblock %}

Iii. models model of ORM

1. ing relationship:

SQL table name ----------- Python Class Name

SQL field --------- class attributes of Python

SQL record --------- Class Object of Python

2. Single Table operations

Class Article (models. model): nid = models. bigAutoField (primary_key = True) title = models. charField (max_length = 50, verbose_name = 'Article title') read_count = models. integerField (default = 0)

(1) Add operation

Views:

 

# Method 1: article_obj = models. article. objects. create (nid = 1, title = "yuan", read_count = 12) # The create method returns the currently created Document Object # Method 2: article_obj = models. article (nid = 1, title = "yuan", read_count = 12) article_obj.save () Delete: models. article. objects. filter (title = "python "). delete () # default cascading deletion modification: models. article. objects. filter (title = "python "). update (read_count = F ("read_count") + 10) query API: <1> all (): Query all results # QuerySet <2> filter (** kwarg S): contains the object that matches the given filter condition # QuerySet <3> get (** kwargs): returns the object that matches the given filter condition, there is only one returned result. If more than one or none of the objects meet the filtering conditions, an error is thrown. # Model object <5> exclude (** kwargs): contains objects that do not match the given filter condition # QuerySet <4> values (* field ): return a ValueQuerySet -- a special QuerySet. After running, the returned result is not a series of model instantiation objects, but an iterative dictionary sequence # QuerySet <9> values_list (* field ): it is very similar to values (). It returns a sequence of tuples, and values returns a dictionary sequence # QuerySet <6> order_by (* field ): sort query results # QuerySet <7> reverse (): reverse sort of query results # QuerySet <8> distinct (): remove duplicate records from the returned results # QuerySet <10> count (): returns the number of objects that match the query (QuerySet) in the database. # Int <11> first (): returns the first record # model object <12> last (): returns the last record # model object <13> exists (): if QuerySet contains data, True is returned; otherwise, False is returned. querySet supports chained operation: models. article. objects. all (). filter (). values (). distinct (). count () QuerySet data type: 1. It can be sliced and can be iterated [obj,...] 2. inert query: articles_list = models. article. objects. all () Use articles_list, such as if articles_list, then convert SQL statement 3. cache mechanism articles_list = models. article. objects. all () for I in articles_list: print (I. title) # hit the database for I in articles_list: print (I. title) # not hit the database ======================================== ============ for I in models. article. objects. all (): print (I. title) # hit the database for I in models. article. objects. all (): print (I. title) # hit the database 4. Optimize the query articles_list = models. article. objects. all (). iterator () for I in articles_list: print (I. title) # hit the database for I in articles_list: print (I. title) # table join operation without result: Table relation: class UserInfo (AbstractUser): # settings: AUTH_USER_MODEL = "blog. userInfo "User Information" "nid = models. bigAutoField (primary_key = True) nickname = models. charField (verbose_name = 'nicknames ', max_length = 32, null = True) telephone = models. charField (max_length = 11, blank = True, null = True, unique = True, verbose_name = 'phone number') avatar = models. fileField (verbose_name = 'Avatar ', upload_to = 'Avatar', default = "avatar/default.png") create_time = models. dateTimeField (verbose_name = 'creation time', auto_now_add = True) class Blog (models. model): "Site Information" nid = models. bigAutoField (primary_key = True) title = models. charField (verbose_name = 'blog title', max_length = 64) site = models. charField (verbose_name = 'blog Postfix ', max_length = 32, unique = True) theme = models. charField (verbose_name = 'blog topic ', max_length = 32) user = models. oneToOneField (to = 'userinfo', to_field = 'nid') class Category (models. model): nid = models. autoField (primary_key = True) title = models. charField (verbose_name = 'category title', max_length = 32) blog = models. foreignKey (verbose_name = 'blog ', to = 'blog', to_field = 'nid') class Article (models. model): nid = models. bigAutoField (primary_key = True) title = models. charField (max_length = 50, verbose_name = 'Article title') desc = models. charField (max_length = 255, verbose_name = 'Article description') # category field: the category object associated with the Article object category = models. foreignKey (verbose_name = 'Article type', to = 'category ', to_field = 'nid', null = True) # user field: user field associated with the Article Object user = models. foreignKey (verbose_name = 'user ', to = 'userinfo', to_field = 'nid') tags = models. manyToManyField (to = "Tag", through = 'article2tag', through_fields = ('Article', 'tag'),) class ArticleDetail (models. model): nid = models. autoField (primary_key = True) content = models. textField (verbose_name = 'Article content',) article = models. oneToOneField (verbose_name = 'Post ', to = 'Article', to_field = 'nid') class Article2Tag (models. model): nid = models. autoField (primary_key = True) article = models. foreignKey (verbose_name = 'Article', to = "Article", to_field = 'nid') tag = models. foreignKey (verbose_name = 'tag', to = "Tag", to_field = 'nid') class Tag (models. model): nid = models. autoField (primary_key = True) title = models. charField (verbose_name = 'tagname', max_length = 32) blog = models. foreignKey (verbose_name = 'blog ', to = 'blog', to_field = 'nid' join table add record operation: 1. Create an article object: user_obj = models. userInfo. objects. get (nid = 1) category_obj = models. category. objects. get (nid = 2) ################# one-to-many relationship binding ################### ####### Method 1: article_obj = models. article. objects. create (nid = 5, title = "Chao Hua Xi "...., user = user_obj, category = category_obj) # Method 2: article_obj = models. article. objects. create (nid = 5, title = "Chao Hua Xi "...., user_id = 1, category_id = 2) ''' Article: nid title user_id category_id 5 '''

 

 

 

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.