class PollCreateForm(forms.Form):
topic = forms.CharField(label='투표 주제', min_length='2', max_length='50')
options = forms.CharField(label='투표 주제 옵션', min_length='2', max_length='300')
<body>
<form action="{% url 'create_poll' %}" method="post">
{% csrf_token %}
{{ create_form }}
</body>
def create_poll(request):
if request.method == 'POST':
form = PollCreateForm(request.POST)
form = PollCreateForm()
context = {
"create_form" : form
}
return render(request, 'pages/create_poll.html', context)
1) create_poll.html
<body>
<form action="{% url 'create_poll' %}" method="post">
{% csrf_token %}
{{ create_form.as_p }}
</body>
CSRF 토큰 : POST 요청을 보낼 때 해킹에 의한 요청을 방어하기 위해서 해커가 유추하기 어려운 무작위의 값을 생성한 뒤 폼 요청을 보낼 때 같이 전송되도록 하고 서버에서 이 값이 정상적으로 들어오는 경우만 유효한 데이터 전송으로 확인하는 방식