从另一个视图调用视图 [英] Call view from another view

查看:97
本文介绍了从另一个视图调用视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图集,其中一个视图为:

I have a viewset with one of the views as:

@list_route(methods=["get"], url_path="special")
def special():
    pass

我从另一个视图中调用此视图,例如:

And I call this view from another view like:

view_fn = viewset.as_view({'get': 'list'})
response = view_fn(request)

但是它没有调用映射到"/special/" special 函数,而是调用了映射到"/" 的函数代码>.我想我需要以某种方式传递 url_path 或使用视图 name 来获取视图?但是,我也不知道该怎么做.

But it does not call my special function which maps to "/special/", instead it calls the function which maps to "/". I guess I need to pass url_path somehow or get the view using view name? However, I'm not sure how to do either.

推荐答案

这将不起作用,因为您需要通过操作来映射该路线.

This will not work because you need to map that route with the action.

目前,您正在将默认列表操作映射到get方法.

In its current form, you are mapping the default list action to the get method.

下面的代码应该起作用:

The following code should work:

view_fn = viewset.as_view({'get': 'special'})

这篇关于从另一个视图调用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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