在DeleteView通用视图中访问kwargs
我正在创建一个需要访问方法外部的 kwargs 的 DeleteView,如下所示:
class DeletePost(DeleteView):
"""
Delete a post. Post deletion is only reserved to space
administrators or site admins.
"""
context_object_name = "get_place"
success_url = '/spaces/' + kwargs['space_name']
def get_object(self):
return get_object_or_404(Post, pk=self.kwargs['post_id'])
但显然,参数和关键字参数不能在方法外部使用。我还尝试在 get 方法中建立 success_url,但 django 无法识别它。如何获取space_name
参数?我试图避免覆盖视图核心方法,如dispatch() 等。
I'm creating a DeleteView which needs to access to kwargs outside of the methods, like this:
class DeletePost(DeleteView):
"""
Delete a post. Post deletion is only reserved to space
administrators or site admins.
"""
context_object_name = "get_place"
success_url = '/spaces/' + kwargs['space_name']
def get_object(self):
return get_object_or_404(Post, pk=self.kwargs['post_id'])
But apparently, arguments and keyword arguments can't be used outside the methods. I also tried to stablish the success_url inside the get method, but django does not recognize it. What can I do to obtain the space_name
parameter? I'm trying to avoid overriding the view core methods like dispatch() and such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
覆盖
get_success_url()
方法。Override the
get_success_url()
method.