-
Python Django 11 - get + post 방식Python Django 2022. 10. 23. 14:32
views.py
from django.shortcuts import render from django.views.generic.base import TemplateView # Create your views here. def mainFunc(request): return render(request, 'index.html') class CallView(TemplateView): template_name="callget.html" def insertFunc(request): return render(request, 'insert.html') def insertFunc2(request): if request.method == 'GET': return render(request, 'insert2.html') elif request.method == 'POST': irum = request.POST.get("name") # java : request.getParameter("name" return render(request, 'list.html', {'myname':irum}) else: print("요청에러") def insertprocess(request): if request.method == 'GET': irum = request.GET.get("name") # java : request.getParameter("name") print(irum) return render(request, 'list.html', {'myname':irum})
insertFunc에서는 get방식으로 insertprocess로 가져가며 거기서 get 값을 빼와서 dict타입으로 호출한다.
insertFunc2에서 get 방식의 경우 insert2.html 로 같은 화면으로 이동하지만 get 방식으로 url을 가져간다.
post 방식의 경우 값을 들고 list.html로 이동하게 만든다.
자식 urls.py
from django.urls import path from gpapp import views urlpatterns = [ path("insert",views.insertFunc), # Functhon views path("insertprocess", views.insertprocess), path("insert2",views.insertFunc2), ]
list.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p>출력 결과 확인 : {{myname}}</p> </body> </html>
insert1.html get 방식
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p>자료 입력</p> <form action="/gptest/insertprocess" method="get"> 이름 : <input type="text" name="name" /> <input type="submit" value="확인"/> </form> </body> </html>
insert2.html post 방식
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p>자료 입력</p> <form action="/gptest/insert2" method="post">{% csrf_token %} 이름 : <input type="text" name="name" /> <input type="submit" value="확인"/> </form> </body> </html>
post의 경우에는 뒤에 {% csrf_token %} 를 무조건 적어야된다. 보안 문제가 있다. 적지 않으면 err를 발생시킨다.
'Python Django' 카테고리의 다른 글
간단한 shop 장바구니 만들기 예제 (0) 2022.10.23 Python Django 12 - session(redirec 방법) (0) 2022.10.23 Python Django 10 - static(css, js, image) (0) 2022.10.23 Python Django 9 - 데이터 이동 forward(DB, 데이터) (0) 2022.10.23 Python Django 8 - controller 사용방법3(Including another URLconf) (0) 2022.10.23