First, Templates filter
Filters are part of the Django template language
Modify variables in a template to display different content
{{value | filter}}
Example: {{list_nums | length}} indicates the length of the list
{{Value | filter | filter | filter}} can be superimposed
Django Template If there is a non-existent variable, will not error, will only give a null value
So modify add_article.html Remove {{% if *%}} Add filter default
<!DOCTYPE HTML><HTML><Head> <title>Add new articles</title></Head><Body><formAction= "{% url ' blog:sub_article '%}"Method= "POST">{% Csrf_token%}<inputtype= ' hidden 'name= ' article_id 'value= "{{article.id | default: ' 0 '}}">article title<inputtype= "text"name= ' title 'value= ' {{Article.title}} '/> <BR>article content<inputtype= "text"name= ' content 'value= ' {{article.content}} '/> <BR> <inputtype= "Submit"value= "Submit"> </form></Body></HTML>
Second, Django Shell
Python interactive command-line program
Automatic introduction of Project environment
You can use it to interact with the project
Start the Django Shell
Pyhton manage.py Shell
Examples of interactions:
From Blog.models Import article
Article.objects.all ()
Effect: 1) Commissioning work 2) test unknown method
Third, admin enhancements
Register the admin configuration class:
Class Articleadmin (admin. Modeladmin)
Registration: Admin.site.register (models. Article,articleadmin)
Show additional fields
List_diplay = (' title ', ' content ') (is the field name in models)
The admin.py is as follows:
from Import Admin from Import Models # Register your models here. class articleadmin (admin. Modeladmin): = ('title','content' ) Admin.site.register (models. Article,articleadmin)
Admin Filter
List_diplay = (' Pub_time ',)
Note that there is only one member of the tuple, add a comma
In models. Article class add release date in py
from Import Models # Create your models here. class article (models. Model): = models. Charfield (max_length=32,default='title') = models. TextField (null=True) = models. Datetimefield (auto_now=True) def__str__(self): return Self.title
Data migration (can only be displayed in the database)
Execute python manage.py makemigrations
Re-executepython manage.py migrate
生成了数据表
To add a filter to the admin.py:
fromDjango.contribImportAdmin from.ImportModels#Register your models here.classarticleadmin (admin. Modeladmin): List_display= ('title','content','Pub_time') List_filter= ('Pub_time',) Admin.site.register (models. Article,articleadmin)
Django Learning (ix)---templates filters and django Shell and admin enhancements