django在CBV中运行另一个基于类的视图(CBV)? [英] django run another class-based view (CBV) in a CBV?

查看:110
本文介绍了django在CBV中运行另一个基于类的视图(CBV)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个CBV(A),CBV(B)和一个像

  regex = r'^ (?P< slug> [ -  \w] +)/(?P< app> [ -  \w] +)'

我想用(A)读取slug和app参数,然后根据这些,将其重定向到适当的CBV,可能(B)。我不想使用HttpResponseRedirect或任何类似的方式重定向用户,而是基本上运行另一个CBV,就像它被调用一样。如何从CBV(A)直接/内部运行另一个CBV(如B)?

解决方案

您可以致电这样:

  class CBViewA(View):
def dispatch(self,request,* args,** kwargs)
如果kwargs ['slug'] =some slug:
return CBViewB.as_view()(request,* args,** kwargs)
else:
返回超级(CBViewA,self).dispatch(request,* args,** kwargs)


so I have a CBV (A), CBV (B), and a url like

regex=r'^(?P<slug>[-\w]+)/(?P<app>[-\w]+)'

I want to read in the slug and app parameters with (A) and then based on those, redirect it to an appropriate CBV, possible (B). I don't want to redirect the user with HttpResponseRedirect or anything like that, but instead basically run another CBV as if it were the one being called. How do I run another CBV, like (B), directly/internally from a CBV (A)?

解决方案

You can call it that way:

class CBViewA(View):
    def dispatch(self, request, *args, **kwargs):
        if kwargs['slug'] = "some slug":
            return CBViewB.as_view()(request, *args, **kwargs)
        else:
            return super(CBViewA, self).dispatch(request, *args, **kwargs)

这篇关于django在CBV中运行另一个基于类的视图(CBV)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆