全局名称'request'未定义:覆盖form_valid [英] global name 'request' is not defined: overriding form_valid
本文介绍了全局名称'request'未定义:覆盖form_valid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
#保护系统免遭url输入攻击
如果用户== self.object .user或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>成功:< / strong>)注释已成功编辑!)
else:
messages.add_message ,message.ERROR,< strong>失败:< / strong>您没有访问级别来编辑该帖子!)
问题是行 messages.add_message(request,message.SUCCESS,< strong> Success:< / strong>该评论已成功编辑!)
似乎请求
的范围超出范围,为什么会这样做?有没有办法解决这个问题?
解决方案
应该是 self.request
而不是请求
。
messages.add_message(self。请求,message.ERROR,...)
I have an UpdateView which I am overriding the form_valid method. For some reason 'request' is shown as being not defined within the overridden method. Here is the full class:
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!")
The problem is the line messages.add_message(request, message.SUCCESS, "<strong>Success:</strong> The comment was edited successfully!")
It seems like the request
is out of scope some how. Why is it doing this? Is there a way to get around this?
解决方案
It should be self.request
and not request
.
messages.add_message(self.request, message.ERROR, "...")
这篇关于全局名称'request'未定义:覆盖form_valid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文