程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django personal blog building --8-- article classification query

編輯:Python

Preface :

The front home page article display can only show all the articles in one brain , The article class of the background database is not classification Exhibition
Now I will show the background articles by category , But before we do that , I need to know what kind of articles my back-end database has , Here is an example of how many types of navigation bars there are : Selected articles 、django Class article 、 Data analysis articles 、 There are four types of important articles , When you click one of the buttons in the navigation bar , You can see all the articles of that type

Code implementation :

First, let's configure the routing :

Everyone should know what this means , Corresponding , Need to be in urls.py Add four lines of code :


After adding these four lines of code, start to write the view function
There is one method we have used many times , That's it filter Method

articleList = Article.objects.filter(category=2)

This means taking out the background Article In the table category( classification ) In the first 2 The data of

This is my category surface , according to id To take a value .

  • Why can't you use category=' Data analysis ' To get the value ?
    Because we use Article Table to query ,Article In the table category Belongs to foreign key , We should use the primary key in the foreign key (id) To query data
  • Why can't you use Category Table to query ?
    because Category The data in the table is only the data of various categories , Pictured above , And we use Article.objects.filter(category=2) What such a statement finds out is category=2 The article

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved