禁止(403)CSRF验证失败

问题描述 投票:0回答:1

当我单击add_tech.html中的“确定”按钮时,它将重定向我在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})

我的网址-

    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', )

我的模型.py-

class Name(models.Model):
    your_name = models.CharField(max_length=100)
django csrf
1个回答
0
投票

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>   
© www.soinside.com 2019 - 2024. All rights reserved.