개발 여행/Django

[Django Admin] 3. Form, Generic View

찰나의꿈 2023. 4. 9. 17:53

html form 도입

<form action="{% url 'polls:vote' question.id %}" method="post">
    {% csrf_token %}
    <fieldset>
        <legend><h1>{{ question.question_text }}</h1></legend>
        {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
        {% for choice in question.choice_set.all %}
            <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
            <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
        {% endfor %}
    </fieldset>
    <input type="submit" value="Vote">
</form>
  • {% csrf_token %} 크로스 오리진 방어 태그. post, put 등의 form이나 서버에 수정을 가할 수 있는 부분을 다른 도메인에서 떼간 다음에 변형해서, 우리 서버에 위조된 신호를 보낼 수 있다. form 태그에 이 토큰을 넣으면 이 부분을 방어해준다.
  • {{ forloop.counter }} 는 몇 번째 루프인지를 알려준다.

view를 수정한다.

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse

from .models import Question, Choice

#...

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay with an error message.
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice."
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # PRG Pattern
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
  • request.POST[’keyname’]을 통해 POST로 온 데이터에 접근 가능하다. GET도 당연히 있다. 이를 통해 POST, GET 등 원하는 HTTP method를 정할 수 있다.
    • POST 정보에 keyname 정보가 없다면 KeyError를 발생시킨다.
  • POST 메서드이므로 PRG패턴을 반드시 사용하자.
  • URL 내에 PathVariable이 있다면 reverse()를 통해 조립이 가능하다. polls:result의 링크를 불러온 뒤, PathVariable 요소에 args(혹은 많다면 kwargs)를 꽂아넣는다.

Generic View

detail(): 특정 정보를 찾아서, detail.html에 넘겨준다.

results(): 특정 정보를 찾아서, results.html에 넘겨준다.

이런 식으로 많이 보이는 패턴들은 Django가 Shortcut을 제공해준다.

URL configuration과 view를 수정하면 된다.

#polls/views.py

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 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'

  • DetailView는 말 그대로 한 요소를 봐야 하므로 pk element를 필요로 한다. 이것은 **URL 단에서 ‘pk’**라는 이름의 Path Variable을 가져오는 것으로 해결이 필요.
  • 어떤 Model에 적용되는지를 알아야 하므로 model 정의
  • Generic View는 Default Template name이 있는데, 이를 덮어쓰려면 따로 template_name을 정의
  • DetailView는 모델이 있으니 Django가 알아서 context 상에서 이름을 추측할 수 있다(question이라는 이름 언급은 그 어디에도 없으나 모델명이 Question이라서 그렇게 전달된다).
  • ListView는 그런 요소가 없으므로 context_object_name을 정해줘야 한다.