Skip to content

Commit 0be3dec

Browse files
committed
feat: Write views
resolved #10 - 뷰 및 뷰 기능 추가 - Http404 에러 코드 작성 - 템플릿 생성 및 render()로 연결 - urls.py에 이름공간 정의
1 parent 4436cc4 commit 0be3dec

16 files changed

+49
-3
lines changed
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
182 Bytes
Binary file not shown.
745 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>{{ question.question_text }}</h1>
2+
<ul>
3+
{% for choice in question.choice_set.all %}
4+
<li>{{ choice.choice_text }}</li>
5+
{% endfor %}
6+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% if latest_question_list %}
2+
<ul>
3+
{% for question in latest_question_list %}
4+
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
5+
{% endfor %}
6+
</ul>
7+
{% else %}
8+
<p>No polls are available.</p>
9+
{% endif %}

mysite/polls/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
from django.urls import path
22
from . import views
33

4+
app_name = 'polls'
45
urlpatterns = [
6+
# ex: /polls/
57
path('', views.index, name='index'),
8+
9+
# ex: /polls/5/
10+
path('<int:question_id>/', views.detail, name='detail'),
11+
12+
# ex: /polls/5/results/
13+
path('<int:question_id>/results/', views.results, name='results'),
14+
15+
# ex: /polls/5/vote/
16+
path('<int:question_id>/vote/', views.vote, name='vote'),
617
]

mysite/polls/views.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
from django.shortcuts import render
2-
from django.http import HttpResponse
1+
from django.shortcuts import render, get_object_or_404
2+
from django.http import HttpResponse, Http404
3+
from django.template import loader
4+
from .models import Question
35

46

57
def index(request):
6-
return HttpResponse("Hello, world! You're at the polls index.")
8+
latest_question_list = Question.objects.order_by('-pub_date')[:5]
9+
context = {'latest_question_list': latest_question_list}
10+
# HttpResponse 객체와 자주 쓰는 표현 단축 : render
11+
return render(request, 'polls/index.html', context)
12+
13+
14+
def detail(request, question_id):
15+
question = get_object_or_404(Question, pk=question_id)
16+
return render(request, 'polls/detail.html', {'question': question})
17+
18+
19+
def results(request, question_id):
20+
response = f"You're looking at the results of question {question_id}"
21+
return HttpResponse(response)
22+
23+
24+
def vote(request, question_id):
25+
return HttpResponse(f"You're voting on question {question_id}")
26+

0 commit comments

Comments
 (0)