问题
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