如何修复CSRF验证失败。请求中止

时间:2019-07-11 18:21:08

标签: django sql-insert csrf-token

我有一个django网站,其中包含一个表格,用于将数据提交到数据库中。

问题是用户单击“提交”按钮,系统显示以下错误:

  

CSRF验证失败。请求中止。

但是我已经在表单内包含了 csrf_token

form.py

from django import forms

from .models import contact


class contactForm(forms.ModelForm):
    class Meta:
        model = contact
        fields = (
            'name',
             'last_name', 
             'father_name', 
             'mother_name', 
             'gender', 
             'content',
              )

models.py

from django.db import models
from django.utils import timezone 
from django.contrib.auth.models import User

# Create your models here.
class contact(models.Model):
    MALE = 1
    FEMALE = 2
    GENDER = (
        (MALE, 'Male'),
        (FEMALE, 'Female'),
    )
    name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    father_name = models.CharField(max_length=120)
    mother_name = models.CharField(max_length=120)
    gender = models.PositiveIntegerField(choices=GENDER,blank=True,null=True)
    content = models.TextField()
    create_date = models.DateTimeField(default=timezone.now,auto_now=False,auto_now_add=False)
    update_date = models.DateTimeField(default=timezone.now,auto_now=False,auto_now_add=False)
    user = models.ForeignKey(User,on_delete=models.CASCADE,default=1)

html页面

<form method="POST" >{% csrf_token %}
    {{form.as_p}}

  <div id='left-column-Input' class="formInput" include="select()"> 
   <div class="forminputs">
   <input type="text" name="name" autocomplete="off" required />
   <label for="name" class="label-name">
    <span class="content-name" name="content-name">first name</span>
   </label></div>
  <div class="forminputs">
   <input type="text" name="name" autocomplete="off" required />
   <label for="name" class="label-name">
    <span class="content-name" name="content-name">last name</span>
   </label></div>
  <div class="forminputs">
   <input type="text" name="name" autocomplete="off" required />
   <label for="name" class="label-name">
    <span class="content-name" name="content-name">father name</span>
   </label></div>
 <button type="submit">Save</button>
</form>

并且没有记录插入数据库。

0 个答案:

没有答案