1.面向对象方法
在views.py中编写
- 引入模块
from django import views
- 函数编写,创建类文件
class View(views.View):
def get(self, request):
print('GET方法')
return HttpResponse('GET方法')
def post(self, request):
print('POST方法')
return HttpResponse('POST方法')
- 配置路由 urls.py中编写
- 引入模块
from django.views.generic import TemplateView
- 配置路由
yyy方法可以让我们不用在views.py中编写函数,直接就可以通过访问yyy方法访问2.html文件
urlpatterns = [
path('xxx', views.View.as_view()),
path('yyy', TemplateView.as_view(template_name='kanyun/2.html'))
]
使用
前端页面,点击发送,表单提交地址为,路由中的xxx,如果是post请求则返回post函数中的结果,如果是g