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

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

问题描述

所以我有一个 CBV (A)、CBV (B) 和一个 url

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

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

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

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)?

推荐答案

你可以这样称呼它:

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天全站免登陆