본문 바로가기

Python_WEB/Django_Tutorial

[Django]Survey WEB Application Tutorial 7

1. style.css 설정

'''
polls/static/polls/style.css
'''
li a {
    color: green;
}

 

2. 정적 파일의 절대 URL 생성

'''
polls/templates/polls/index.html
'''
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">

 

3. 배경 이미지 추가

'''
polls/static/polls/style.css
'''
body {
    background: white url("images/background.gif") no-repeat;
}

폴더 위치 : polls/static/polls/images/

 

 

4. 관리자 폼 커스터마이징

'''
polls/admin.py
'''
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)

 

5. 폼을 fieldset으로 분활

'''
polls/admin.py
'''
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Question, QuestionAdmin)

 

6. Choice를 관리자에게 등록

'''
polls/admin.py
'''
from django.contrib import admin

from .models import Choice, Question
# ...
admin.site.register(Choice)

 

7. register() 호출을 제거

'''
polls/admin.py
'''
from django.contrib import admin

from .models import Choice, Question


class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)

 

8. ChoiceInline 변경

'''
polls/admin.py
'''
class ChoiceInline(admin.TabularInline):
    #...

 

9.  list_display admin 옵션을 사용.(객체의 변경 목록 페이지에서 열로 표시 할 필드 이름들의 튜플)

'''
polls/admin.py
'''
class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date')

 

10. was_published_recently () 메소드도 포함

'''
polls/admin.py
'''
class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date', 'was_published_recently')

 

11. was_published_recently에 대한 열 머리글은 기본적으로 메서드 이름 (밑줄을 공백으로 대체)이며 

    각 줄에는 출력의 문자열 표현이 포함

'''
polls/models.py
'''
class Question(models.Model):
    # ...
    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?'

 

12. DIRS 옵션을 TEMPLATES 설정 추가

'''
mysite/settings.py
'''
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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',
            ],
        },
    },
]

 

13. 자신의 사이트 이름으로 변경

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
{% endblock %}

 

https://docs.djangoproject.com/ko/3.0/intro/tutorial07/

 

첫 번째 장고 앱 작성하기, part 7 | Django 문서 | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com