Skip to content

Commit

Permalink
Writing your first Django app, part 7
Browse files Browse the repository at this point in the history
  • Loading branch information
ot-nemoto committed Jun 21, 2024
1 parent d08ae51 commit dab2101
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
},
"[markdown]": {
"prettier.tabWidth": 2
},
"[html]": {
"editor.formatOnSave": false,
"editor.defaultFormatter": null
}
}
2 changes: 1 addition & 1 deletion mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down
21 changes: 19 additions & 2 deletions polls/admin.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions polls/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

from django.contrib import admin
from django.db import models
from django.utils import timezone

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "admin/base.html" %}

{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

{% block branding %}
<div id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></div>
{% if user.is_anonymous %}
{% include "admin/color_theme_toggle.html" %}
{% endif %}
{% endblock %}

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

0 comments on commit dab2101

Please sign in to comment.