Python Django

Python Django 7 - controller 사용방법2(class-based views)

코딩탕탕 2022. 10. 23. 14:23

 

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
]

views.py에서 TemplateView를 상속받은 클래스를 만든다.

 

 

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})

TemplateView 객체 안에 template_name이라는 메소드가 있는데 요청명을 리턴해준다.

 

 

callget.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>GET / POST 연습</p>
<a href="/gptest/insert">자료 입력</a>
<br />
<a href="/">메인 페이지</a>
</body>
</html>

 

 

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>