diff --git a/mysite/settings.py b/mysite/settings.py index e23d57e..877f5d5 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -55,15 +55,15 @@ ROOT_URLCONF = 'mysite.urls' TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [BASE_DIR / "templates"], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, diff --git a/polls/admin.py b/polls/admin.py index 33937e0..f7f2a32 100644 --- a/polls/admin.py +++ b/polls/admin.py @@ -1,5 +1,22 @@ from django.contrib import admin -from .models import Question +from .models import Choice, Question -admin.site.register(Question) \ No newline at end of file + +class ChoiceInline(admin.TabularInline): + model = Choice + extra = 3 + + +class QuestionAdmin(admin.ModelAdmin): + fieldsets = [ + (None, {"fields": ["question_text"]}), + ("Date information", {"fields": ["pub_date"], "classes": ["collapse"]}), + ] + list_display = ["question_text", "pub_date", "was_published_recently"] + inlines = [ChoiceInline] + list_filter = ["pub_date"] + search_fields = ["question_text"] + + +admin.site.register(Question, QuestionAdmin) \ No newline at end of file diff --git a/polls/models.py b/polls/models.py index 9e34096..1528cf3 100644 --- a/polls/models.py +++ b/polls/models.py @@ -2,12 +2,22 @@ import datetime from django.db import models from django.utils import timezone +from django.contrib import admin class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField("date published") + def was_published_recently(self): + now = timezone.now() + return now - datetime.timedelta(days=1) <= self.pub_date <= now + + @admin.display( + boolean=True, + ordering="pub_date", + description="Published recently?", + ) def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date <= now diff --git a/polls/templates/admin/base_site.html b/polls/templates/admin/base_site.html new file mode 100644 index 0000000..7997e66 --- /dev/null +++ b/polls/templates/admin/base_site.html @@ -0,0 +1,12 @@ +{% extends "admin/base.html" %} + +{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} + +{% block branding %} +
Polls Administration Page
+{% if user.is_anonymous %} + {% include "admin/color_theme_toggle.html" %} +{% endif %} +{% endblock %} + +{% block nav-global %}{% endblock %} \ No newline at end of file