对以下Django视图的POST请求导致(403)CSRF验证失败。我已经确认隐藏的csrf标记在页面的源代码中呈现。我也能够在其他视图中无错误地发出POST请求。我不确定如何进一步调试。
views.py:
def email(request):
if request.method == 'POST':
email = request.POST['email']
fd = open('emaildb.csv','a')
fd.write(email+',somefile\n')
fd.close()
return render(request, 'receipts/email.html', {'text':'Thanks, we''ll get back to you soon.'})
else:
return render(request, 'receipts/email.html',)
email.html:
<form action="email" method="post" enctype="text/plain">
{% csrf_token %}
E-mail:<br>
<input type="text" name="email">
<input type="submit" value="Send">
</form>
答案 0 :(得分:0)
你无法调用那样的方法
<form action="email" method="post" enctype="text/plain">
{% csrf_token %}
E-mail:<br>
<input type="text" name="email">
<input type="submit" value="Send">
</form>
在你的urls.py中你必须写
url(r'^email/$', views.email, name='email')
并使用类似
的形式调用此方法<form action="{% url 'your_folder_name_where_located_your_urls.py:email'%}" method="post" enctype="text/plain">
{% csrf_token %}
E-mail:<br>
<input type="text" name="email">
<input type="submit" value="Send">
</form>