Call view from another view

我怕爱的太早我们不能终老 提交于 2021-02-20 04:07:45

问题


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)

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.


回答1:


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

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'})


来源:https://stackoverflow.com/questions/43858582/call-view-from-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!