Skip to content

Commit d7a8fd9

Browse files
committed
docs: Add 07-customize-admin.md
1 parent fadab2c commit d7a8fd9

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

docs/07-customize-admin.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 07. Custiomize Admin
2+
3+
## 관리자 폼 커스터마이징
4+
-`mysite/settings.py`
5+
```python
6+
TEMPLATES = [
7+
{
8+
...,
9+
'DIRS': [BASE_DIR / 'templates'],
10+
...,
11+
}
12+
]
13+
```
14+
15+
- `polls/admin.py`
16+
```python
17+
from django.contrib import admin
18+
19+
from .models import Choice, Question
20+
21+
22+
class ChoiceInline(admin.TabularInline):
23+
model = Choice
24+
extra = 3
25+
26+
27+
class QuestionAdmin(admin.ModelAdmin):
28+
fieldsets = [
29+
(None, {'fields': ['question_text']}),
30+
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
31+
]
32+
inlines = [ChoiceInline]
33+
list_display = ('question_text', 'pub_date', 'was_published_recently')
34+
list_filter = ['pub_date']
35+
search_fields = ['question_text']
36+
37+
38+
admin.site.register(Question, QuestionAdmin)
39+
admin.site.register(Choice)
40+
41+
```
42+
43+
- `polls/models.py`
44+
```python
45+
from django.contrib import admin
46+
47+
48+
class Question(models.Model):
49+
question_text = models.CharField(max_length=200)
50+
pub_date = models.DateTimeField('date Published')
51+
52+
def __str__(self):
53+
return self.question_text
54+
55+
@admin.display(
56+
boolean=True,
57+
ordering='pub_date',
58+
description='Published recently?',
59+
)
60+
61+
# def was_published_recently(self):
62+
# return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
63+
def was_published_recently(self):
64+
now = timezone.now()
65+
return now - datetime.timedelta(days=1) <= self.pub_date <= now
66+
```
67+
68+
![img_1.png](images/img_1.png)
69+
![img_2.png](images/img_2.png)
70+
71+
## 관리자 템플릿 커스터마이징
72+
```
73+
django-tutorial/venv/lib/site-packages/django/contrib/admin/templates/admin/base_site.html
74+
polls/templates/admin/base_site.html 로 복사
75+
```
76+
```html
77+
{% extends "admin/base.html" %}
78+
79+
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
80+
81+
{% block branding %}
82+
<h1 id="site-name"><a href="{% url 'admin:index' %}">Administration</a></h1>
83+
{% endblock %}
84+
85+
{% block nav-global %}{% endblock %}
86+
```
87+
![img_1.png](images/img_3.png)
88+
89+
- 기타 수정하고자 하는 것은 templates 폴더로 복사해서 수정하면 됨.

docs/images/img_1.png

62.1 KB
Loading

docs/images/img_2.png

58.2 KB
Loading

docs/images/img_3.png

48.6 KB
Loading

0 commit comments

Comments
 (0)