一、项目静态文件
1.1 创建css文件
在polls目录下创建static/polls/style.css
并写入以下内容:
li a {
color: green;
}
li a:hover {
color: red;
}
1.2 引入css文件
在index.html文件中添加:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
1.3 重启server,查看效果
当鼠标悬浮在第三项时:
1.4 添加背景
在polls/static/polls/目录中新建images文件夹。
并在其中放置一张background.gif
图片。
在style.css中添加:
body {
background: white url("images/background.gif") no-repeat;
}
重启django访问主页即可查看:
二、修改后台界面
2.1 修改polls/admin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
('main', {'fields':['question_text']}),
('Date information', {'fields':['pub_date']}),
]
admin.site.register(Question, QuestionAdmin)
修改后的后台界面如图:
再次更改polls/admin.py
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Question, QuestionAdmin)
通过 TabularInline
(替代 StackedInline
),关联对象以一种表格式的方式展示,显得更加紧凑:
再次修改polls/admin.py中的QuestionAdmin函数:
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
2.2 添加过滤器
修改QuestionAdmin如下图:
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
添加之后如下图:
在过滤器后面再加一条:
search_fields = ['question_text']
效果如下图: