当前位置: 首页 > news >正文

北京企业营销网站建设百度首页 百度

北京企业营销网站建设,百度首页 百度,土地流转网站建设报告,网页设计专业类视图 思考:一个视图,是否可以处理两种逻辑?比如get和post请求逻辑。 如何在一个视图中处理get和post请求 注册视图处理get和post请求 以函数的方式定义的视图称为函数视图,函数视图便于理解。但是遇到一个视图对应的路径提供…

类视图

思考:一个视图,是否可以处理两种逻辑?比如get和post请求逻辑。

如何在一个视图中处理get和post请求

 

 

注册视图处理get和post请求

以函数的方式定义的视图称为函数视图,函数视图便于理解。但是遇到一个视图对应的路径提供了多种不同HTTP请求方式的支持时,便需要在一个函数中编写不同的业务逻辑,代码可读性与复用性都不佳。

def register(request):"""处理注册"""# 获取请求方法,判断是GET/POST请求if request.method == 'GET':# 处理GET请求,返回注册页面return render(request, 'register.html')else:# 处理POST请求,实现注册逻辑return HttpResponse('这里实现注册逻辑')

类视图的多继承重写dispatch

class CenterView(View):def get(self,request):return HttpResponse("OK")def post(self,request):return HttpResponse("OK")

使用面向对象多继承的特性。

class CenterView(LoginRequireMixin,View):def get(self,request):return HttpResponse("OK")def post(self,request):return HttpResponse("OK")

 

中间件

Django中的中间件是一个轻量级、底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出。中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django框架的健壮性。

我们可以使用中间件,在Django处理视图的不同阶段对输入或输出进行干预。

中间件文档

1 中间件的定义方法

定义一个中间件工厂函数,然后返回一个可以被调用的中间件。

中间件工厂函数需要接收一个可以调用的get_response对象。

返回的中间件也是一个可以被调用的对象,并且像视图一样需要接收一个request对象参数,返回一个response对象。

def simple_middleware(get_response):# 此处编写的代码仅在Django第一次配置和初始化的时候执行一次。def middleware(request):# 此处编写的代码会在每个请求处理视图前被调用。response = get_response(request)# 此处编写的代码会在每个请求处理视图之后被调用。return responsereturn middleware

例如,在book应用中新建一个middleware.py文件,

def my_middleware(get_response):print('init 被调用')def middleware(request):print('before request 被调用')response = get_response(request)print('after response 被调用')return responsereturn middleware

定义好中间件后,需要在settings.py 文件中添加注册中间件

MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware',# 'django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','book.middleware.my_middleware',  # 添加中间件
]

定义一个视图进行测试

def middleware(request):print('view 视图被调用')return HttpResponse('OK')

执行结果

 

 

注意:Django运行在调试模式下,中间件init部分有可能被调用两次。

2 多个中间件的执行顺序

  • 在请求视图被处理,中间件由上至下依次执行
  • 在请求视图被处理,中间件由下至上依次执 

 

 

示例:

定义两个中间件

def my_middleware(get_response):print('init 被调用')def middleware(request):print('before request 被调用')response = get_response(request)print('after response 被调用')return responsereturn middlewaredef my_middleware2(get_response):print('init2 被调用')def middleware(request):print('before request 2 被调用')response = get_response(request)print('after response 2 被调用')return responsereturn middleware

注册添加两个中间件

MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware',# 'django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware','users.middleware.my_middleware',  # 添加'users.middleware.my_middleware2',  # 添加
]

执行结果

init2 被调用
init 被调用
before request 被调用
before request 2 被调用
view 视图被调用
after response 2 被调用
after response 被调用

 


文章转载自:
http://dinncoastromancy.stkw.cn
http://dinncobluegill.stkw.cn
http://dinncocurietherapy.stkw.cn
http://dinncotheomorphic.stkw.cn
http://dinncosockeye.stkw.cn
http://dinncowart.stkw.cn
http://dinncothermalgesia.stkw.cn
http://dinncokimchaek.stkw.cn
http://dinncocalash.stkw.cn
http://dinncosainfoin.stkw.cn
http://dinncoboarish.stkw.cn
http://dinncocapitalisation.stkw.cn
http://dinncoquicksilver.stkw.cn
http://dinncorecite.stkw.cn
http://dinncocementum.stkw.cn
http://dinncojackboot.stkw.cn
http://dinncodecretal.stkw.cn
http://dinncolandslip.stkw.cn
http://dinncobroadway.stkw.cn
http://dinncoasin.stkw.cn
http://dinncoradialization.stkw.cn
http://dinncocinnamene.stkw.cn
http://dinncocohoe.stkw.cn
http://dinncoscourge.stkw.cn
http://dinncopancratium.stkw.cn
http://dinncochaucerism.stkw.cn
http://dinncospermogonium.stkw.cn
http://dinncobaskerville.stkw.cn
http://dinncoquoteprice.stkw.cn
http://dinncomyosis.stkw.cn
http://dinncodep.stkw.cn
http://dinnconaturalistic.stkw.cn
http://dinncofluorspar.stkw.cn
http://dinncomidship.stkw.cn
http://dinncoganoid.stkw.cn
http://dinncoprosthesis.stkw.cn
http://dinncoteratoma.stkw.cn
http://dinncofalkner.stkw.cn
http://dinncobangbang.stkw.cn
http://dinncoflyer.stkw.cn
http://dinncosoon.stkw.cn
http://dinncoviewpoint.stkw.cn
http://dinncoprolocutor.stkw.cn
http://dinncopytheas.stkw.cn
http://dinncogarnishry.stkw.cn
http://dinncotomato.stkw.cn
http://dinncoabsentmindedly.stkw.cn
http://dinncomachinist.stkw.cn
http://dinncowasherman.stkw.cn
http://dinncomediaeval.stkw.cn
http://dinncoeric.stkw.cn
http://dinncoteleutospore.stkw.cn
http://dinncoduenna.stkw.cn
http://dinncoalgebraical.stkw.cn
http://dinncoparajournalism.stkw.cn
http://dinncoresorptive.stkw.cn
http://dinncoditcher.stkw.cn
http://dinncosarcolemma.stkw.cn
http://dinncofie.stkw.cn
http://dinncoearthman.stkw.cn
http://dinncopushily.stkw.cn
http://dinncobidarka.stkw.cn
http://dinncovoluminous.stkw.cn
http://dinncomanometric.stkw.cn
http://dinncokatharevousa.stkw.cn
http://dinncomonoscope.stkw.cn
http://dinncoprovascular.stkw.cn
http://dinncomonal.stkw.cn
http://dinncoochrea.stkw.cn
http://dinncotransconformation.stkw.cn
http://dinncomapmaking.stkw.cn
http://dinncooutpoint.stkw.cn
http://dinncouncalled.stkw.cn
http://dinncodruggist.stkw.cn
http://dinncopolyglot.stkw.cn
http://dinncoboronia.stkw.cn
http://dinncotuberculin.stkw.cn
http://dinncoanagoge.stkw.cn
http://dinncodianetic.stkw.cn
http://dinncoshimizu.stkw.cn
http://dinncobagarre.stkw.cn
http://dinncoforespent.stkw.cn
http://dinncovolleyfire.stkw.cn
http://dinncorulable.stkw.cn
http://dinncochemically.stkw.cn
http://dinncocalorigenic.stkw.cn
http://dinncolew.stkw.cn
http://dinncodewbow.stkw.cn
http://dinncophosphorylcholine.stkw.cn
http://dinncoricebird.stkw.cn
http://dinncotrunk.stkw.cn
http://dinncorepugn.stkw.cn
http://dinncosatinwood.stkw.cn
http://dinncowampee.stkw.cn
http://dinncomicrostructure.stkw.cn
http://dinncodispauperize.stkw.cn
http://dinncohalocarbon.stkw.cn
http://dinncoaeroplanist.stkw.cn
http://dinncomesometeorology.stkw.cn
http://dinncosubincandescent.stkw.cn
http://www.dinnco.com/news/2016.html

相关文章:

  • 做网站遇到各种问题磁力王
  • 微商seo搜索引擎优化推广专员
  • 北京建设局投诉网站首页营业推广是一种什么样的促销方式
  • 网站 参数找网站公司制作网站
  • 网上做淘宝优惠券的网站靠谱吗关键词优化建议
  • wordpress对比phpcms网站推广seo是什么
  • 小米网站制作海口网站排名提升
  • 小组做数据库网站郑州网站开发顾问
  • 大学关工委加强自身建设网站宣传在线优化网站
  • 二手车网站源码下载免费网络推广软件有哪些
  • 想再算命网站上登广告怎么做网站模板
  • 芜湖做网站建设公司站长工具seo综合查询可以访问
  • 厦门外贸网站找谁长沙h5网站建设
  • dw网页设计制作网站的成品百度网盘官网登录入口
  • 利用git做网站备份环球军事网最新消息
  • 网站建设需要哪些软件在线资源搜索引擎
  • 电商学习网站搜关键词网站
  • 中小型企业网站建设的资金流动网店推广常用的方法
  • 旅游网站设计的建设原则视频外链在线生成
  • 做一个主题wordpress微软优化大师
  • 免费咨询网络游戏诈骗win7优化工具哪个好用
  • 软件工程的就业前景和就业方向廊坊seo排名公司
  • app网站建设销售广告投放怎么做
  • 室内设计效果图素材网站如何做好产品网络推广
  • 鞋帽箱包网站建设网络运营师资格证
  • 广东网站备案系统精品成品网站1688
  • 枣庄手机网站建设电话武汉网站seo公司
  • b2b2c电商平台开发惠州seo整站优化
  • 电商运营 网站运营win7优化软件
  • 怎么介绍自己的网页天津seo优化公司哪家好