UpdateView - 对象没有属性“对象”
UpdateView - object has no attribute 'object
我想验证更新 post 的用户是 post 的作者。
我的代码如下所示(相关部分):
class UpdatePostView(UpdateView):
model = Post
fields = ['body']
template_name = 'forum/post_update_form.html'
def dispatch(self, *args, **kwargs):
if self.request.user != self.object.author:
return HttpResponseForbidden()
return super(CreatePostView, self).dispatch(*args, **kwargs)
我遇到的错误:
'UpdatePostView' object has no attribute 'object'
在这一点上,我有点困惑,因为文档说:When using UpdateView you have access to self.object, which is the object being updated.
我错过了什么?
你覆盖得太早了。 dispatch
是在设置 self.object
之前调用的第一个方法。您需要覆盖 get
和 post
并检查每个(在调用 super
之后),或者在您的调度方法中显式调用 get_object
。
我想验证更新 post 的用户是 post 的作者。
我的代码如下所示(相关部分):
class UpdatePostView(UpdateView):
model = Post
fields = ['body']
template_name = 'forum/post_update_form.html'
def dispatch(self, *args, **kwargs):
if self.request.user != self.object.author:
return HttpResponseForbidden()
return super(CreatePostView, self).dispatch(*args, **kwargs)
我遇到的错误:
'UpdatePostView' object has no attribute 'object'
在这一点上,我有点困惑,因为文档说:When using UpdateView you have access to self.object, which is the object being updated.
我错过了什么?
你覆盖得太早了。 dispatch
是在设置 self.object
之前调用的第一个方法。您需要覆盖 get
和 post
并检查每个(在调用 super
之后),或者在您的调度方法中显式调用 get_object
。