3-6. 폼

오서영·2022년 5월 14일
0
post-thumbnail
  1. PollCreateForm 구현하기
class PollCreateForm(forms.Form):
topic = forms.CharField(label='투표 주제', min_length='2', max_length='50')
options = forms.CharField(label='투표 주제 옵션', min_length='2', max_length='300')
  1. create_poll.html 템플릿 만들기
<body>
<form action="{% url 'create_poll' %}" method="post">
{% csrf_token %}
{{ create_form }}
</body>
  1. create_poll에서 POST 데이터 처리하기
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. as_p 사용해서 출력해보기

1) create_poll.html

<body>
<form action="{% url 'create_poll' %}" method="post">
{% csrf_token %}
{{ create_form.as_p }}
</body>
  1. CSRF 토큰

CSRF 토큰 : POST 요청을 보낼 때 해킹에 의한 요청을 방어하기 위해서 해커가 유추하기 어려운 무작위의 값을 생성한 뒤 폼 요청을 보낼 때 같이 전송되도록 하고 서버에서 이 값이 정상적으로 들어오는 경우만 유효한 데이터 전송으로 확인하는 방식

profile
개발과 보안에 관심 있는 대학생입니다😎

0개의 댓글