【问题标题】:UpdateView - object has no attribute 'objectUpdateView - 对象没有属性“对象”
【发布时间】:2015-06-06 02:23:00
【问题描述】:

我想验证更新帖子的用户是帖子的作者。

我的代码如下所示(相关部分):

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.

我错过了什么?

【问题讨论】:

    标签: python django django-views


    【解决方案1】:

    你太早了。 dispatch 是在设置 self.object 之前调用的第一个方法。您需要覆盖 getpost 并检查每一个(在调用 super 之后),或者在调度方法中显式调用 get_object

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多