Summarize django knowledge points

1. View functions: Request Object------------- request: 1...

1. View functions:

Request Object------------- request:

1,HttpRequest.body:) Request original data

2,HttpRequest.path:) A string representing the requested path component (without domain name)

3,HttpRequest.method

4,HttpRequest.GET

5,HttpRequest.POST

6,HttpRequest.FILES

7,HttpResquest.user:) An AUTH_USER_Objects of type MODEL

- Response object:

return. HttpResponse(")] Returns an instance of a string

return render(request,"template", {":"}) return string instance

return Redirect ("/index/") Redirect

2. Template Language

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

views:

def foo (request):

name="li"

l=[111,2222,3333]

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

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

Variable: {}

------- Deep query period character. For example: {

------- Filter {}

2. Tags:

for loop

{%for i in l%}

{}

{%empty%}

<p>There are no qualifications </p>

{%endfor%}

if statement

{%if name="li"%}

<p> Yes</p>

{%elif%}

{%endif....%}

3. Template Syntax

(1) In masterBase.html: {%block con%}{%endblock%}

(2) Inherit the masterIndex.html:

{%extends"Base.html"%}" Inheritance

{%block con%}

{%endblock%}

3. models Model of ORM

1. Mapping relationship:

Table Name in sql----------Class Name of Python

Fields in sql--------Class properties of Python

Records in sql------Class objects for Python

2. Form operation

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) # create Method returns the currently created article 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 Cascade Delete //Modify: models.Article.objects.filter(title="python").update(read_count=F("read_count")+10) //Query API: <1> all(): Query all results #QuerySet <2> filter(**kwargs): It contains objects that match the given filter criteria #QuerySet <3> get(**kwargs): Returns the object that matches the filter criteria given. There is one and only one result. //An error is thrown if more than one or none of the objects meet the filter criteria.# model object <5> exclude(**kwargs): It contains objects that do not match the given filter criteria #QuerySet <4> values(*field): Return a ValueQuerySet-A special QuerySet,What you get after running is not a series model Instantiate an object, but an iterative sequence of dictionaries #QuerySet <9> values_list(*field): It is associated with values()Very similar, it returns a tuple sequence. values A dictionary sequence is returned #QuerySet <6> order_by(*field): Sort query results #QuerySet <7> reverse(): Reverse sort of query results #QuerySet <8> distinct(): Remove duplicate records from returned results #QuerySet <10> count(): Return matching queries in database(QuerySet)Number of objects. # int <11> first(): Return the first record # model object <12> last(): Return to the last record # model object <13> exists(): If QuerySet Contains data, returns True,Otherwise return False querySet Support chain operation: models.Article.objects.all().filter().values().distinct().count() QuerySet Data type: 1,Sliceable, iterative [obj,....] 2,Lazy queries: articles_list=models.Article.objects.all() //Using articles_list, such as if articles_list, convert the sql statement at this time 3,Caching 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 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) # No result //Join Table Operation: //Table relations: class UserInfo(AbstractUser): # settings: AUTH_USER_MODEL = "blog.UserInfo" """ //User Information """ nid = models.BigAutoField(primary_key=True) nickname = models.CharField(verbose_name='Nickname?', 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='Head portrait', 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='Personal Blog Title', max_length=64) site = models.CharField(verbose_name='Personal Blog Suffix', max_length=32, unique=True) theme = models.CharField(verbose_name='Blog Theme', 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='Owning 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: and Article Object-Associated category object category = models.ForeignKey(verbose_name='Article Type', to='Category', to_field='nid', null=True) # user Field: and Article Object-Associated user field user = models.ForeignKey(verbose_name='Owned 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='Owned Articles', 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='Label', to="Tag", to_field='nid') class Tag(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='Label Name', max_length=32) blog = models.ForeignKey(verbose_name='Owning Blog', to='Blog', to_field='nid' //Add record operation to associated table: 1,Create an article for: user_obj=models.UserInfo.objects.get(nid=1) category_obj=models.Category.objects.get(nid=2) #################One-to-many relationship binding######################### # Mode 1: article_obj=models.Article.objects.create(nid=5,title="Pick up flowers at night"....,user=user_obj,category=category_obj) # Mode 2: article_obj=models.Article.objects.create(nid=5,title="Pick up flowers at night"....,user_id=1,category_id=2) ''' Article: nid title user_id category_id 5 Pick up flowers at night 1 2 '''

19 July 2020, 10:57 | Views: 5140

Add new comment

For adding a comment, please log in
or create account

0 comments