[英]How can I reuse a DRF class-based views POST method?
Now I want to reuse the post method of BundleList.现在我想重用BundleList的post方法。 Either I find out the absolute URL and use
requests.post(URL)
to send a request.要么我找出绝对的 URL 并使用
requests.post(URL)
发送请求。
The 2nd way is to reuse by return BundleList.as_view()(request)
in a view function.第二种方法是通过在视图 function 中
return BundleList.as_view()(request)
来重用。 But I can't set request.data = data
.但我不能设置
request.data = data
。 Request data is immutable.请求数据是不可变的。
When I try to use当我尝试使用
url = reverse_lazy(BundleList.as_view(), request=request)
print(f"{url = }")
It just gives me:它只是给了我:
NoReverseMatch at /generateSampleBundle/
Reverse for 'my_app.views.BundleList' not found. 'my_app.views.BundleList' is not a valid view function or pattern name.
The BundleList is a class-based view with get and post method. BundleList 是一个基于类的视图,带有 get 和 post 方法。
drfurlpatterns = [ # DRF URL endpoints
path('bundles/', views.BundleList.as_view()),
]
Can anyone help me out?谁能帮我吗?
You should set name
for your view and use that name in reverse_lazy()
您应该为视图设置
name
并在reverse_lazy()
中使用该名称
for ex:例如:
drfurlpatterns = [ # DRF URL endpoints
path('bundles/', views.BundleList.as_view()), name='bundle-list'
]
then然后
url = reverse_lazy('bundle-list', request=request)
Also, to know why request.data
is sometimes immutable see this question另外,要知道为什么
request.data
有时是不可变的, 请参阅这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.