-
Python Django 7 - controller 사용방법2(class-based views)Python Django 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>
'Python Django' 카테고리의 다른 글
Python Django 9 - 데이터 이동 forward(DB, 데이터) (0) 2022.10.23 Python Django 8 - controller 사용방법3(Including another URLconf) (0) 2022.10.23 Python Django 6 - controller 사용방법 1(Functhon views) (0) 2022.10.23 Python Django 5 - 이클립스 이외의 app 추가 설정 (0) 2022.10.23 Python Django 4 - 어플리케이션(App) 설정 추가 + Templates (0) 2022.10.23