인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

zxcvv75771916's profile image
zxcvv75771916

asked

<M.B.I.T> Create a test page! with Django

Creating templates 3

선생님..... 이런 에러가 ..

Written on

·

262

0

HTML/CSSjavascriptdjango

Answer 5

1

hujun23489530님의 프로필 이미지
hujun23489530
Instructor

views.py의 result 함수에서 request.POST를 print 해보시고 아래와 같이 'question-1' 키값이 들어가 있는지 확인해보세요.

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

네 오늘 저녁에 해보겠습니다!! 

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

키 값이 안들어가있으면 어떻게 하면 좋을까요.......???

hujun23489530님의 프로필 이미지
hujun23489530
Instructor

그렇다면 form 부분을 잘못 작성하신 것입니다.

(영상을 다시 보시고 작성하셔야 할 것 같네요.ㅜㅜ)

csrfmiddlewaretoken은 제대로 들어왔나요?

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

헉 폼......제가 그..... 올려주신 설명 사이트에 그대로 복붙 했는데ㅠㅠㅠㅠ

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>form</title>

</head>

<body>

  <form action="/result/" method="post">

    {% csrf_token %}

    {% for question in questions %}

      <h3>{{ question.number }}번 {{ question.content }}</h3>

      {% for choice in question.choice_set.all %}

        <div>

          <input type="radio" name="question-{{ question.pk }}" id="choice-{{ choice.pk }}" value="{{ choice.developer.pk }}">

          <label for="choice-{{ choice.pk }}">{{ forloop.counter }}. {{ choice.content }}</label>

        </div>

      {% endfor %}

      <hr>

    {% endfor %}

    <input type="submit" value="제출하기">

  </form>

</body>

</html>

hujun23489530님의 프로필 이미지
hujun23489530
Instructor

csrfmiddlewaretoken은 제대로 들어왔나요?

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

제가 코린이라 무슨말씀이신지 이해가 안갑니다 ㅠ

hujun23489530님의 프로필 이미지
hujun23489530
Instructor

첨부해드린 이미지에 csrfmiddlewaretoken 값이 있어요. 이게 콘솔창에서 출력이 되고 있는지를 검사해 달라는 말씀이었습니다. :)

이는 views.py에서 해당 함수의 requests 값이 제대로 들어오고 있는지 확인하는 것이에요. 만약 제대로 들어오고 있지 않으면 html파일 문제가 아니라 views.py 파일 문제이기 때문에 그렇습니다.

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

from django.shortcuts import render

from .models import Question, Developer, Choice

def index(request):

    developers = Developer.objects.all()

    

    context = {

        'developers': developers,

    }

    

    return render(request, 'index.html', context=context)

def form(request):

    questions = Question.objects.all()

    context = {

        'questions': questions,

    }

    

    return render(request, 'form.html', context)

def result(request):

    

    # 문항 수

    N = Question.objects.count()

    # 개발자 유형 수

    K = Developer.objects.count()

    

    # 개발유형마다 몇 개인지 저장할 리스트 counter[1] = (1번 유형 점수(개수))

    counter = [0] * (K + 1)

    

    for n in range(1, N+1):

        developer_id = int(request.POST[f'question-{n}'][0])

        counter[developer_id] += 1

        print(request.POST)

        # 최고점 개발 유형

    best_developer_id = max(range(1, K + 1), key=lambda id: counter[id])

    best_developer = Developer.objects.get(pk=best_developer_id)

    best_developer.count += 1

    best_developer.save()

    

    context = {

        'developer': best_developer,

        'counter': counter,

    }

    

    return render(request, 'result.html', context)

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

선생님 뷰 코드인데 한번 확인 부탁드립니다 ㅠ-ㅠ 

아 이코드는 . . 영상강의에서 받아 적은대로 안되서

노션 페이지에서 올려둔 zip 파일에 저장되어 있는 코드를 가져왔습니다 

0

hujun23489530님의 프로필 이미지
hujun23489530
Instructor

https://inf.run/uLoD 여기에 답변 남겨드렸습니다

0

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

키값이 13부터 들어가있어요 .... 몰까요 

0

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

0

zxcvv75771916님의 프로필 이미지
zxcvv75771916
Questioner

form/페이지에서 선택하고 제출하기 누르면

결과페이지가 떠야하는데 에러가 납니다 ㅠ 

zxcvv75771916's profile image
zxcvv75771916

asked

Ask a question