当我单击add_tech.html中的ok按钮时,它将在upload_type.html上重定向我。但它在单击“确定”按钮时会显示错误。
错误-
禁止(403) CSRF验证失败。请求中止。帮助失败的原因: CSRF令牌丢失或不正确。
我的模板(add_tech.html)-
<form action="/uploads/type/" method="post">
<label for="your_name">New Tech: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
</form> 我的模板(upload_type.html)-
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form}}
</form>我的View.py -
def upload_type(request):
if request.method =='POST':
details = NameForm(request.POST)
if details.is_valid():
return render(request, "core/upload_type.html", {'form':details})
else:
details = NameForm()
return render(request, 'core/upload_type.html', {'form': details})我的Url.py -
urlpatterns = [
url(r'^uploads/type/$', views.upload_type, name='upload_type'),]我的form.py -
from uploads.core.models import Name
class NameForm(forms.ModelForm):
class Meta:
model = Name
fields = ('your_name', )我的模特-
class Name(models.Model):
your_name = models.CharField(max_length=100)发布于 2020-01-17 10:28:39
在django模板中,为了post方法,您需要像这样的csrf令牌
<form action="/uploads/type/" method="post">
{% csrf_token %}
<label for="your_name">New Tech: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
</form> 发布于 2020-01-17 10:34:17
对于POST请求,需要csrf令牌。因此,在模板中添加{% csrf_token %}。
<form action="/uploads/type/" method="post">
{% csrf_token %}
<label for="your_name">New Tech: </label>
<input id="your_name" type="text" name="your_name" value="{{ current_name }}">
<input type="submit" value="OK">
</form> 来自文档
Django提供了易于使用的保护,以防止跨站点请求伪造。通过POST提交表单时启用了CSRF保护,您必须使用csrf_token模板标记,如前面的示例所示。
https://stackoverflow.com/questions/59785507
复制相似问题