[Django]장고 공식튜토리얼 따라하기-Part3&Part4

Cherry·2022년 1월 14일
0
post-thumbnail

Part 3

1. View가 실제로 뭔가를 하도록 만들기

  • HttpResponse - 객체를 반환

    #polls.views.py
    from django.http import HttpResponse
    ...
      return HttpResponse(output)
  • render() - HttpResponse 객체와 함께 돌려주는 구문을 쉽게 표현할 수 있도록 하는 단축 기능

    #polls.views.py
    from django.shortcuts import render
    ...
    return render(request, 'polls/index.html', context)
  • Http404 - 404에러 일으키기

    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})
  • get_object_or_404() - Http404 예외를 발생시키는 단축기능

    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})

2. 템플릿에서 하드코딩된 URL 제거

  • 템플릿에 링크를 적으면, 이 링크는 다음과 같이 부분적으로 하드코딩된다
    #polls/index.html 
    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
  • polls.urls 모듈의 path() 함수에서 인수의 이름을 정의했으므로, {% url %} template 태그를 사용하여 url 설정에 정의된 특정한 URL 경로들의 의존성을 제거할 수 있다.
    <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

Part 4

1. form 요소 사용

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% 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 %}
<input type="submit" value="Vote">
</form>
  • form으로 제출된 데이터를 처리하고 그 데이터로 vote() 함수를 사용하여 정보를 저장한다.
    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']) # 선택된 설문의 ID를 문자열로 반환
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

2. 제너릭 뷰 사용하기

  • 제너릭 뷰는 일반적인 패턴을 추상화하여 앱을 작성하기 위해 Python 코드를 작성하지 않아도 된다.
  • ListView - 개체 목록 표시 추상화
      ```
      class IndexView(generic.ListView):
      template_name = 'polls/index.html' # "polls/index.html" 템플릿을 사용하기 위해 ListView 에 template_name 를 전달
      context_object_name = 'latest_question_list'
    
      def get_queryset(self):
          """Return the last five published questions."""
          return Question.objects.order_by('-pub_date')[:5]
     ```
  • DetailView - 특정 개체 유형에 대한 세부 정보 페이지 표시
    class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html' #  <app name>/<model name>_detail.html 템플릿을 사용

0개의 댓글