From 7c4da46d7302067a9c076695e9ad64a6bb24b9d9 Mon Sep 17 00:00:00 2001 From: kjg Date: Fri, 17 Jul 2020 22:33:39 +0900 Subject: [PATCH] [Django #8] Tutorial 7 --- kjg/django/mysite/mysite/settings.py | 2 +- kjg/django/mysite/polls/admin.py | 19 +++++++++++++++++-- kjg/django/mysite/polls/models.py | 6 ++++++ .../polls/templates/admin/base_site.html | 9 +++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 kjg/django/mysite/polls/templates/admin/base_site.html diff --git a/kjg/django/mysite/mysite/settings.py b/kjg/django/mysite/mysite/settings.py index ec0bcd2..97b8565 100644 --- a/kjg/django/mysite/mysite/settings.py +++ b/kjg/django/mysite/mysite/settings.py @@ -55,7 +55,7 @@ ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/kjg/django/mysite/polls/admin.py b/kjg/django/mysite/polls/admin.py index ffd3760..88e96f7 100644 --- a/kjg/django/mysite/polls/admin.py +++ b/kjg/django/mysite/polls/admin.py @@ -2,5 +2,20 @@ from django.contrib import admin from .models import Question, Choice -admin.site.register(Question) -admin.site.register(Choice) +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + + +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'] + +admin.site.register(Question, QuestionAdmin) +#admin.site.register(Choice) diff --git a/kjg/django/mysite/polls/models.py b/kjg/django/mysite/polls/models.py index a7c5580..06bfcdc 100644 --- a/kjg/django/mysite/polls/models.py +++ b/kjg/django/mysite/polls/models.py @@ -15,6 +15,12 @@ class Question(models.Model): def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date <= now + 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 = 'Published recently?' #def was_published_recently(self): #return self.pub_date >= timezone.now() - datetime.timedelta(days=1) diff --git a/kjg/django/mysite/polls/templates/admin/base_site.html b/kjg/django/mysite/polls/templates/admin/base_site.html new file mode 100644 index 0000000..9587878 --- /dev/null +++ b/kjg/django/mysite/polls/templates/admin/base_site.html @@ -0,0 +1,9 @@ +{% extends "admin/base.html" %} + +{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} + +{% block branding %} +

Polls Administration

+{% endblock %} + +{% block nav-global %}{% endblock %}