Django - 在模板中使用了 {% csrf_token %}
Django - {% csrf_token %} was used in a template
提交后,我收到错误 400 和以下消息:
create_foo.html:
<form class="modal-form" method="post" action="{% url 'create_foo'> %}">
{% csrf_token %}
{{ forms }}
</form>
create_foo.py:
@require_POST
@login_required(login_url='/login/')
def create_foo(request):
#
#
if form.is_valid():
return HttpResponse('Success', status=201)
return JsonResponse({'html': render_to_string('create_foo.html', {'forms': form}), 'message': 'Failed'}, status=400)
我遇到错误 400 时遇到问题
我有一个带有消息错误的表单。
我无法改进表格上的数据并继续,并且出现错误:
/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py:67: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not
using RequestContext.
"A {% csrf_token %} was used in a template, but the context "
警告是因为您在调用 render_to_string
时没有包含 request
。将其更改为:
render_to_string('create_foo.html', {'forms': form}, request=request)
提交后,我收到错误 400 和以下消息:
create_foo.html:
<form class="modal-form" method="post" action="{% url 'create_foo'> %}">
{% csrf_token %}
{{ forms }}
</form>
create_foo.py:
@require_POST
@login_required(login_url='/login/')
def create_foo(request):
#
#
if form.is_valid():
return HttpResponse('Success', status=201)
return JsonResponse({'html': render_to_string('create_foo.html', {'forms': form}), 'message': 'Failed'}, status=400)
我遇到错误 400 时遇到问题 我有一个带有消息错误的表单。
我无法改进表格上的数据并继续,并且出现错误:
/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py:67: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not
using RequestContext.
"A {% csrf_token %} was used in a template, but the context "
警告是因为您在调用 render_to_string
时没有包含 request
。将其更改为:
render_to_string('create_foo.html', {'forms': form}, request=request)