본문 바로가기

Django 맛보기

Django 맛보기5 - 뷰 만들기

안녕하세요 개발자 정씨입니다.

꽤 많이 했다고 생각했는데, 아직 갈길이 멀군요. 더 열심히 해봐야겠습니다.

 

1. 뷰(View) 추가하기

# polls/views.py

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

뷰(View)는 Django의 웹페이지의 한 종류라고합니다.

위와 같이 polls/views.py를 수정하여 뷰를 추가하고

 

# polls/urls.py

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

polls/urls.py를 수정하여 뷰들을 url로 연결해주십시다.

 

Url에 잘 연결되어서 뷰들이 정상적으로 보이는것을 확인 할 수 있습니다.

 

 

2. 뷰(View)에 기능 추가하기

# polls/views.py

from django.http import HttpResponse

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

# Leave the rest of the views (detail, results, vote) unchanged

views.py의 index 함수를 위와 같이 변경하여, 질문 항목을 보여줄 수 있는 기능을 추가해봅시다.

 

이전 과정에서 추가했던 "What's up?"이라는 질문과

관리자페이지를 통해 제가 임의로 추가한 "How are you going?"이라는 질문이 보입니다.

 

 

3. Templates 활용하기

# polls/templates/polls/index.html

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

위와 같은 html파일을 추가하여 템플릿을 만들어봅시다.

 

# polls/views.py

from django.http import HttpResponse
from django.template import loader

from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))

그리고 views.py의 index함수를 위와 같이 수정하여 해당 템플릿을 사용해봅시다.

 

# polls/views.py

from django.shortcuts import render

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

수정을 완료하면, 위와 같이 템플릿이 사용된 페이지를 확인 할 수 있습니다.

또한 a태그를 이용해 detail함수로 이동하게끔 만든 기능도 정상적으로 동작함을 확인 할 수 있습니다.

 

# polls/views.py

from django.shortcuts import render

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

템플릿에 context 를 채워넣어 표현한 결과를 HttpResponse 객체와 함께 돌려주는 구문은 자주 사용할 것 같습니다.

이에 Django에서는 이런 표현을 쉽게 할 수 있도록 단축 기능을 제공합니다.

위의 내용은 단축 기능 중 render 함수를 활용한 예시입니다.

 

 

4. 404 예외처리

# polls/views.py

from django.http import Http404
from django.shortcuts import render

from .models import Question
# ...
def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})

 

# polls/templates/polls/detail.html

{{ question }}

HTTP 404 에러에 대한 예외처리를 위와 같이 처리할 수 있습니다.

 

# polls/views.py

from django.shortcuts import get_object_or_404, render

from .models import Question
# ...
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

위와 같이 단축기능의 get_object_or_404() 함수를 사용하면 편리하게 해당 기능을 구현 할 수 있습니다.

 

 

5. URL 하드코딩 제거

# polls/index.html

# 수정전
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

# 수정후
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

index.html 템플릿에서 a태그의 url이 하드코딩으로 되어 있는 부분을 확인 할 수 있습니다.

하지만, urls.py에서 명명된 name을 기반으로 위와 같은 수정이 가능합니다.

 

# polls/urls.py

app_name = 'polls'
urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

 

# polls/templates/polls/index.html

# 수정전
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

# 수정후
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>

urls.py에 app_name을 추가하게 되면 위와 같이 사용하는 것도 가능합니다.

 

 

점점 APP이 구색을 갖춰지고 있는 것 같습니다. 최종적으로 어떤 모습을 보일지 궁금하네요

댓글을 통한 커뮤니케이션은 언제나 환영입니다. 감사합니다.