I have been getting this error referring to this method in my views.py file:
在我的视图中引用这个方法时,我得到了这个错误。py文件:
def AddNewUser(request):
a=AMI()
if(request.method == "POST"):
print(request.POST)
# print(request['newUser'])
# print(request['password'])
return render_to_response("ac/AddNewUser.html", {})
But my other functions work just fine. It's just this button in my HTML file that doesn't work.
但是我的其他功能还可以。只是HTML文件中的这个按钮不工作。
<form name="AddNewUser" action="/ac/AddNewUser" method="post"> {% csrf_token %} <input type="submit" name="addNewUser" id="addNewUser" value="Create User"></form>
<表单名称="addnewuser" action="/ac/AddNewUser" 方法="post"> {% csrf_token %}
As you can see I've got the {% csrf_token %} but it's still not working. I also know some people are having this problem if they don't have MIDDLEWARE_CLASSES in their settings.py but I have that inserted correctly. What could be causing this problem? The only other line in the error says: "The view function uses RequestContext for the template, instead of Context." But I don't know what that could mean.
如您所见,我有{% csrf_token %},但它仍然不能工作。我也知道,有些人如果在他们的设置中没有middleware_class,就会出现这个问题。py,但我已经正确插入了。是什么导致了这个问题?在这个错误中,唯一的另一个行是:“视图函数使用请求上下文作为模板,而不是上下文。”但我不知道这意味着什么。
2 个解决方案
#1
11
You have to use a RequestContext object to get the context, then pass the results in to your render_to_response() function. RequestContext adds in a required CSRF token.
您必须使用RequestContext对象来获取上下文,然后将结果传递给render_to_response()函数。RequestContext添加一个必需的CSRF令牌。
from django.template import RequestContext
from django.shortcuts import render_to_response
csrfContext = RequestContext(request)
return render_to_response(some_template, csrfContext)
As a side note, you can also use RequestContext to add contexts/dictionaries intended for the template. For instance, I frequently use:
另外,还可以使用RequestContext添加用于模板的上下文/字典。例如,我经常使用:
initialData = {'form': theForm, 'user_status': 'online'}
csrfContext = RequestContext(request, initialData)
return render_to_response(show_template, csrfContext)
As a (brief) explanation of what RequestContext does: most middleware creates something called a context processor, which is simply a function that supplies a context (dictionary) of variables. RequestContext looks for all the available context processors, gets their contexts, and appends them all to a single (giant) context.
(简单地)解释RequestContext的作用:大多数中间件都创建称为上下文处理器的东西,它只是提供变量上下文(字典)的函数。RequestContext查找所有可用的上下文处理器,获取它们的上下文,并将它们附加到单个(巨大的)上下文。
#2
2
NOTE: The RequestContext has to be used in both the view that serves the form as well as the view that receives the post. If you follow directions above and still doesn't work, this might be the problem! It was for me.
注意:RequestContext必须在服务表单的视图和接收post的视图中使用。如果你按照上面的指示去做,但仍然没有成功,这可能就是问题所在!这是对我来说。
#1
11
You have to use a RequestContext object to get the context, then pass the results in to your render_to_response() function. RequestContext adds in a required CSRF token.
您必须使用RequestContext对象来获取上下文,然后将结果传递给render_to_response()函数。RequestContext添加一个必需的CSRF令牌。
from django.template import RequestContext
from django.shortcuts import render_to_response
csrfContext = RequestContext(request)
return render_to_response(some_template, csrfContext)
As a side note, you can also use RequestContext to add contexts/dictionaries intended for the template. For instance, I frequently use:
另外,还可以使用RequestContext添加用于模板的上下文/字典。例如,我经常使用:
initialData = {'form': theForm, 'user_status': 'online'}
csrfContext = RequestContext(request, initialData)
return render_to_response(show_template, csrfContext)
As a (brief) explanation of what RequestContext does: most middleware creates something called a context processor, which is simply a function that supplies a context (dictionary) of variables. RequestContext looks for all the available context processors, gets their contexts, and appends them all to a single (giant) context.
(简单地)解释RequestContext的作用:大多数中间件都创建称为上下文处理器的东西,它只是提供变量上下文(字典)的函数。RequestContext查找所有可用的上下文处理器,获取它们的上下文,并将它们附加到单个(巨大的)上下文。
#2
2
NOTE: The RequestContext has to be used in both the view that serves the form as well as the view that receives the post. If you follow directions above and still doesn't work, this might be the problem! It was for me.
注意:RequestContext必须在服务表单的视图和接收post的视图中使用。如果你按照上面的指示去做,但仍然没有成功,这可能就是问题所在!这是对我来说。