我们为一个web应用增加新功能时的步骤是:修改model.py,更新数据库,然后编写视图函数来处于http请求,接着修改模板用于显示内容。如果增加了新的模板,就要增加 新的url。
Our article table now has the title, the article content, and the creation time, which is certainly far from enough, Now we are going to add more content to the article table ~ The following is the changed content, we added the modification time, the classification, and the summary, the classification is as a foreign key. A foreign key is a one-to-many form in which an article corresponds to a category, and a category can have multiple articles, and we also create another category of tables.
Class article (models. Model): title = models. Charfield (' title ', max_length=100) content = models. TextField (' text ') create_time = models. Datetimefield (' Creation time ', auto_now_add=true) last_modified_time = models. Datetimefield (' Modified time ', auto_now=true) abstract = models. Charfield (' abstract ', max_length=54, blank=true, null=true, help_text= "optional, if empty extracts the first 54 characters of the body") category = models. ForeignKey (' category ', verbose_name= ' category ', & NBsp; null=true, on_delete=models. Set_null) def __unicode__ (self): Return self.title class meta: #Meta包含许多的选项, here the ordering represents sort, '-' denotes reverse order, we sort by creation time, You can also sort ordering = ['-create_time '] class by modifying the time category (models. Model): name = models. Charfield (' Class name ', max_length=20) create_time = models. Datetimefield (' Creation time ', auto_now_add=true) last_modified = models. Datetimefield (' x Modified Time ', auto_now=true) def __unicode__ (self): return selF.name
Then we update the database:
Python manage.py makemigrations
Since the new modification time does not provide a default value, when updating the database will prompt you to add a default value, follow the prompts to modify it.
Python manage.py Migrate
Next modify the view, because we have added a classified table, we want to read all the articles from the database, so we add an entry to the dictionary returned by the index function:
#encoding: Utf-8from django.shortcuts import renderfrom .models import article, Category #我们要先导入我们新增的这个表def index (Request): article_list = article.objects.all () category_list = category.objects.all (). Order_by (' name ') return render (request, ' article/index.html ', {' Article_ List ': article_list, ' category_list ': category_list})
We open the background, in the project directory, there is a manage.py directory Open command line, run
Python manage.py runserver
Visiting 127.0.0.1:8000, we found that nothing changed, and we went backstage to look down and visit 127.0.0.1:8000/admin/
We enter the article editing interface, we will see the bottom of the two more things, one is a summary, one is classified, but the classification of what all wood has, then how do we add classification? Only use the category in the admin backstage register it good ~
From Django.contrib import adminfrom. Models import article, Categoryfrom. Forms Import blogform# This blogform is the Pagedownclass blogadmin (Admin) that we mentioned at the beginning of our last blog. Modeladmin): Form = BlogFormadmin.site.register (article, blogadmin) Admin.site.register (Category)
Then we can create an entry and add an entry to the article ~
Refine the article table, add categories, and detail pages