本文介绍了尽管{%csrf_token%},CSRF验证仍失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尽管我正在使用{%csrf_token%},但CSRF验证仍失败。错误在哪里?
I get CSRF verification failed despite the fact that I am using {% csrf_token %}. Where is mistake?
<html>
<head>
<title>Name</title>
</head>
<body>
<h1>Tasks</h1>
<form action="" method="post">
{{ form.as_p }}
<input type="submit" name="add" value="add">
{% for a in comments %}
<h3>{{ a.body}}</h3>
<input type="submit" name="delete" value="delete" />
<input type="hidden" name="idcomment" id="{{a.id}}" value="{{a.id}}"/>
{% csrf_token %}
</form>
{% endfor %}
</body>
</html>
推荐答案
可能的解决方案:
<html>
<head>
<title>Name</title>
</head>
<body>
<h1>Tasks</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="add" value="add">
{% for a in comments %}
<h3>{{ a.body}}</h3>
<input type="submit" name="delete" value="delete" />
<input type="hidden" name="idcomment" id="{{a.id}}" value="{{a.id}}"/>
{% endfor %}
</form>
</body>
</html>
另一种解决方案
from django.shortcuts import render
#your view
context = {}
return render(request, 'your_file.html', contest)
这篇关于尽管{%csrf_token%},CSRF验证仍失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!