Notice
Recent Posts
Recent Comments
ยซ   2024/09   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฒ์ž๋ผ๋‚˜๋Š”์ฒญ๋…„

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ4(์ œ๋„ˆ๋ฆญ ๋ทฐ) ๋ณธ๋ฌธ

django

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ4(์ œ๋„ˆ๋ฆญ ๋ทฐ)

JihyunLee 2019. 9. 17. 16:31
๋ฐ˜์‘ํ˜•

https://docs.djangoproject.com/ko/2.2/intro/tutorial04/

 

์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ, part 4 | Django ๋ฌธ์„œ | Django

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

docs.djangoproject.com

 

๋‹จ์ˆœํ•˜๊ฒŒ list๋ฅผ ๋ณด์—ฌ์ฃผ๊ฑฐ๋‚˜, detail์„ ๋ณด์—ฌ์ฃผ๋Š” ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” generic view๋ผ๊ณ  ํ•ด์„œ, 

"URL์—์„œ ์ „๋‹ฌ ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ ์˜ค๋Š” ๊ฒƒ๊ณผ ํ…œํ”Œ๋ฆฟ์„ ๋กœ๋“œํ•˜๊ณ  ๋ Œ๋”๋ง ๋œ ํ…œํ”Œ๋ฆฟ์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ธฐ๋ณธ ์›น ๊ฐœ๋ฐœ์˜ ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค" ๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ์ž์ฃผ ์“ฐ์ด๋Š” ๋ถ€๋ถ„์„, ์žฅ๊ณ ๋Š” generic view๋กœ ๋งŒ๋“ค์–ด ๋‘์—ˆ๋‹ค.

 

์ด๊ฒƒ์„ ์‚ฌ์šฉ ํ•˜๋Š”๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

1. URLconf์ˆ˜์ •

 

1
2
3
4
5
6
7
8
9
10
11
from django.urls import path
 
from . import views
 
app_name = 'polls'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]
cs

 

.as_view()๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ถ™์–ด์•ผํ•œ๋‹ค.

 

2. view ์ˆ˜์ •

์ด์ „์—๋Š” ํ•จ์ˆ˜๊ฐ€ ์ž‘๋™ํ•ด์„œ url๊ณผ ํ…œํ”Œ๋ฆฟ์„ ์—ฐ๊ฒฐํ–ˆ๋Š”๋ฐ ์ง€๊ธˆ์€ class๊ฐ€ ์ž‘๋™ํ•œ๋‹ค.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
 
from .models import Choice, Question
 
 
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'
 
    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]
 
 
class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'
 
 
class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'
cs
๋ฐ˜์‘ํ˜•