Django中CBV(类通用视图)和FBV(视图函数)详细解释
发布时间:2020-12-15 17:13:08 所属栏目:大数据 来源:网络整理
导读:FBV、CBV是Django视图路由处理模型,当用户请求送达路由系统URL后,由其转发给视图view来分析并处理 CBV全称是 class base views ,中文名字类通用视图 FBV全称是 function base views ,中文名字视图函数 一、FBV实例: urls.py from?django.conf.urls?impo
FBV、CBV是Django视图路由处理模型,当用户请求送达路由系统URL后,由其转发给视图view来分析并处理 CBV全称是class base views,中文名字类通用视图 FBV全称是function base views,中文名字视图函数 一、FBV实例: urls.py from?django.conf.urls?import?url,?include #?from?django.contrib?import?admin from?mytest?import?views ? urlpatterns?=?[ ????#?url(r‘^admin/‘,?admin.site.urls),????url(r‘^index/‘,?views.index),] views.py from?django.shortcuts?import?render ? ? def?index(request): ????if?request.method?==?‘POST‘: ????????print(‘method?is?:‘?+?request.method) ????elif?request.method?==?‘GET‘: ????????print(‘method?is?:‘?+?request.method) ????return?render(req,?‘index.html‘) index.html <!DOCTYPE?html> <html> <head> ????<meta?charset="UTF-8"> ????<title>index</title> </head> <body> ????<form?action=""?method="post"> ????????<input?type="text"?name="A"?/> ????????<input?type="submit"?name="b"?value="提交"?/> ????</form> </body> </html> 二、CBV实例 urls.py from?mytest?import?views ? urlpatterns?=?[ ????#?url(r‘^index/‘,?views.Index.as_view()),] 注:url(r‘^index/‘,views.Index.as_view()),?是固定用法。 views.py from?django.views?import?View ? ? class?Index(View): ????def?get(self,?request): ????????print(‘method?is?:‘?+?request.method) ????????return?render(request,?‘index.html‘) ? ????def?post(self,51); max-height: 35em; position: relative; widows: 1; margin-top: 0px !important;'><!DOCTYPE?html> <html> <head> ????<meta?charset="UTF-8"> ????<title>index</title> </head> <body> ????<form?action=""?method="post"> ????????<input?type="text"?name="A"?/> ????????<input?type="submit"?name="b"?value="提交"?/> ????</form> </body> </html> 推荐一个辅助 学习开发 cbv 工程的网站:ccbv。结合 Django 源码学习 cbv 非常清晰。 参考:http://zmrenwu.com/post/33/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |