Python Django
Python Django 8 - controller 사용방법3(Including another URLconf)
코딩탕탕
2022. 10. 23. 14:25
가장 많이 사용되는 방법이다.
부모 urls.py
from django.contrib import admin
from django.urls import path
from gpapp import views
from gpapp.views import CallView
from django.urls.conf import include
urlpatterns = [
path("admin/", admin.site.urls),
path("",views.mainFunc), # Functhon views
path('gpapp/callget', CallView.as_view()), # Class-based views
path('gptest/', include('gpapp.urls')) # Including another URLconf
]
gptest/는 밑의 action 명과 같게 짓는다. 각자 생성한 어플리케이션(app) 안에 자식 urls.py를 만들 수 있다. 메인 urls.py가 받으면 그것을 자식 urls.py에게 위임시킨다.
자식 urls.py
from django.urls import path
from gpapp import views
urlpatterns = [
path("insert",views.insertFunc), # Functhon views
path("insertprocess", views.insertprocess),
]
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 insertprocess(request):
irum = request.GET.get("name") # java : request.getParameter("name")
print(irum)
return render(request, 'list.html', {'myname':irum})
이곳에 insertprocess 함수를 만든 뒤 값이 있으면 담아서 리턴 시킨다.
insert.html
<!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>
list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>출력 결과 확인 : {{myname}}</p>
</body>
</html>