加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django每次请求都使用相同的类视图实例吗?

发布时间:2020-12-20 11:55:33 所属栏目:Python 来源:网络整理
导读:在 django中,当使用基于类的视图时,设置类级变量(例如template_name)是很常见的 class MyView(View): template_name = 'index.html' def get(self,request): ... 我想知道是否在运行时修改这些变量 class MyView(View): template_name = 'index.html' def ge
在 django中,当使用基于类的视图时,设置类级变量(例如template_name)是很常见的

class MyView(View):

      template_name = 'index.html'

      def get(self,request):
          ...

我想知道是否在运行时修改这些变量

class MyView(View):

      template_name = 'index.html'

      def get(self,request):
          if some_contrived_nonce_function(): # JUST SO IT ONLY RUNS ONCE
             self.template_name = 'something.html'
          ...

将仅持续该请求(每个请求创建一个新的MyView实例),或者它将持续所有后续请求(使用相同的MyView实例)

解决方法

修改为:

self.template_name = 'something.html'

肯定只会持续该请求.

修改为:

type(self).template_name = 'something.html'

将导致新实例继承您的更改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读