全局名称 'request' 未定义:覆盖 form_valid

global name 'request' is not defined: overriding form_valid

我有一个 UpdateView,我正在覆盖 form_valid 方法。由于某种原因 'request' 显示为未在重写方法中定义。这是完整的课程:

 class UpdateTopic(UpdateView):
        model = Post
        slug_field = 'pk'
        slug_url_kwarg = 'pk'
        form_class = CommentForm
        template_name = "forums/update_topic.html"

        def form_valid(self, form): 
            user = self.request.user 
            rep = self.request.user.player.get_rep_total

            # protect the system against url input attacks
            if user == self.object.user or rep >=2500:
                self.object = form.save(commit=False)
                self.object.updated_by = self.request.user 
                self.object.save()
                messages.add_message(request, message.SUCCESS, "<strong>Success:</strong> The comment was edited successfully!")
            else:
                messages.add_message(request, message.ERROR, "<strong>Failed:</strong> You don't have the access level to edit that post!")

问题是行 messages.add_message(request, message.SUCCESS, "<strong>Success:</strong> The comment was edited successfully!") request 似乎超出了范围。为什么要这样做?有办法解决这个问题吗?

应该是self.request而不是request

messages.add_message(self.request, message.ERROR, "...")