From dab2101e6b534088fa279febaddb3d14b4178615 Mon Sep 17 00:00:00 2001 From: ot-nemoto <41175383+ot-nemoto@users.noreply.github.com> Date: Fri, 21 Jun 2024 23:05:47 +0900 Subject: [PATCH] Writing your first Django app, part 7 --- .vscode/settings.json | 4 ++++ mysite/settings.py | 2 +- polls/admin.py | 21 +++++++++++++++++++-- polls/models.py | 6 ++++++ templates/admin/base_site.html | 12 ++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 templates/admin/base_site.html diff --git a/.vscode/settings.json b/.vscode/settings.json index 6ee24c2..f071e0f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,5 +12,9 @@ }, "[markdown]": { "prettier.tabWidth": 2 + }, + "[html]": { + "editor.formatOnSave": false, + "editor.defaultFormatter": null } } diff --git a/mysite/settings.py b/mysite/settings.py index 12cc4ce..8410c5b 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -55,7 +55,7 @@ TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [], + "DIRS": [BASE_DIR / "templates"], "APP_DIRS": True, "OPTIONS": { "context_processors": [ diff --git a/polls/admin.py b/polls/admin.py index 6af8ff6..846afa4 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) + +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) diff --git a/polls/models.py b/polls/models.py index 8a03478..1d232d3 100644 --- a/polls/models.py +++ b/polls/models.py @@ -1,5 +1,6 @@ import datetime +from django.contrib import admin from django.db import models from django.utils import timezone @@ -11,6 +12,11 @@ class Question(models.Model): def __str__(self): return self.question_text + @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/templates/admin/base_site.html b/templates/admin/base_site.html new file mode 100644 index 0000000..6aa80af --- /dev/null +++ b/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
+{% if user.is_anonymous %} + {% include "admin/color_theme_toggle.html" %} +{% endif %} +{% endblock %} + +{% block nav-global %}{% endblock %} \ No newline at end of file