<formaction="/emaillist/add"method="">
First name: <inputtype="text"name="fn"value=""><br>
Last name: <inputtype="text"name="ln"value=""><br>
Email address: <inputtype="text"name="email"value=""><br><inputtype="submit"value="submit"></form>
<formaction="/emaillist/add"method="post"><!-- 여기에 추가 -->
First name: <inputtype="text"name="fn"value=""><br>
Last name: <inputtype="text"name="ln"value=""><br>
Email address: <inputtype="text"name="email"value=""><br><inputtype="submit"value="submit"></form>
위의 코드로 처리하면 form이 있는 page에 hidden으로 CSRF데이터가 생긴다.
이 처리는 상당히 보안에 취약하다.
해당 페이지의 html을 파싱해서 페이지의 csrfmiddlewaretoken값의 value를 가져와 form data와 함께 전송하면, 여전히 공격을 받기 쉬운 처리이다.
이는 장고를 좀 더 공부한 뒤, 좀 더 보안적으로 처리해보도록 하자
일단 토큰 처리를 마쳤으니, 다시 접근
성공!
[2] 받은 data, DB에 insert하기
emaillist/views.py
fromemaillist.modelsimportEmaillistfromdjango.httpimportHttpResponseRedirectdefadd(request):emaillist=Emaillist()emaillist.first_name=request.POST['fn']emaillist.last_name=request.POST['ln']emaillist.email=request.POST['email']emaillist.save()# insert 후에는 꼭 redirect 처리!
returnHttpResponseRedirect('/emaillist')
http://localhost:8888/emaillist/form에서 submit을 한 뒤
DBeaver에서 확인해보기
성공!
[3] DB data select 가져오기
sample data를 몇 개 넣어보고 확인해보자!
emaillist/views.py
index에서 모든 데이터를 select해와서 list보여주기!
defindex(request):emaillist=Emaillist.objects.all().order_by('-id')# for email in emaillist:
# print(email)
data={'emaillist':emaillist}returnrender(request,'emaillist/index.html',data)