장고 공식 문서 polls 앱 만들기 코드 오류 공유합니다.

장고 공식문서에 있는 polls 앱 만들기 소스 코드를 따라 하다가 아주 사소한 오류를 발견해서 퀴즈를 냅니다. (사실 이게 오류인지도 확실치는 않습니다. 다만 제 파이참에서는 소스코드 중 한 부분을 수정해야만 정상적으로 돌아가네요.)

장고 공식 문서에 있는 소스코드와 웹주소 링크를 공유하오니 오류를 찾아보세요. (힌트 : vote 함수에서 뭘 하나 없애면 됩니다.)

Django Project Writing your first Django app, part 4

  • polls/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse

from .models import Choice, Question


# ...
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 the question voting form.
        return render(
            request,
            "polls/detail.html",
            {
                "question": question,
                "error_message": "You didn't select a choice.",
            },
        )
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))

hello_world 236

M 2023년 7월 7일 1:12 오전

목록으로
1개의 답변이 있습니다. 1 / 1 Page

정답은 함수 중간에 있는 불필요한 쉼표 하나를 빼면 됩니다. 이 쉼표를 빼야지만 오류가 안나네요.

hello_world

M 2023년 7월 8일 1:00 오전