修改polls/admin.pypython
from django.contrib import admin
from .models import Question, Choice
# Register your models here.
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
# Choice對象要在Question後臺頁面編輯,默認提供3個足夠的選項字段
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('日期信息', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']
admin.site.register(Question, QuestionAdmin)
# 建立一個模型後臺類,接着將其做爲第二個參數傳給admin.site.register()
django
修改polls/models.pyspa
import datetime
from django.utils import timezone
from django.db import models
# Create your models here.
# 在咱們的polls應用程序中,
# 將建立兩個模型:Question和Choice,
# Question有一個問題和一個出版日期,
# Choice有兩個領域:選擇的文本和票數,
# 每一個Choice都關聯一個Question
class Question(models.Model):
question_text = models.CharField(max_length=200, verbose_name='問題文本')
pub_date = models.DateTimeField('出版日期')
def __str__(self):
return self.question_text
class Meta:
verbose_name = '問題'
verbose_name_plural = '問題'
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = '最近發佈?'
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200, verbose_name='選擇文本')
votes = models.IntegerField(default=0, verbose_name='選票')
def __str__(self):
return self.choice_text
class Meta:
verbose_name = '選擇'
verbose_name_plural = '選擇'
server
python manage.py runserver對象
啓動服務ip
http://127.0.0.1:8000/polls/get