django字段在提交表单时需要erroe

django字段在提交表单时需要erroe,django,Django,我正试图提交我的cabinform,但它一次又一次地在客舱代码中显示“此字段是必需的”,甚至所有字段都已填写。请帮助我,我被卡住了。我在客舱型号代码中使用了外键,这似乎会导致问题吗?你能告诉我我做错了什么吗?提前谢谢 In models.py class Centre(models.Model): name= models.CharField(max_length=50, blank=False, unique=True) address = models.CharFiel

我正试图提交我的cabinform,但它一次又一次地在客舱代码中显示“此字段是必需的”,甚至所有字段都已填写。请帮助我,我被卡住了。我在客舱型号代码中使用了外键,这似乎会导致问题吗?你能告诉我我做错了什么吗?提前谢谢

In models.py

class Centre(models.Model):
     name= models.CharField(max_length=50, blank=False, unique=True)
     address = models.CharField(max_length =250)
     phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                             message="Phone number must be entered in the format: '+999999999'. Up to 10 digits allowed.")
     contact = models.CharField(max_length=100, blank=False)
     phone = models.CharField(validators=[phone_regex], max_length=10, blank=True)  # validators should be a list
     slug = models.SlugField(unique=False)


class Cabin(models.Model):
    random_string = str(random.randint(100000, 999999))
    centre_name = models.ForeignKey(Centre, on_delete=models.CASCADE )
    code = models.CharField(max_length=6, blank=False, unique=True,                 default=random_string)
      total_seats = models.IntegerField(blank='False')


 views.py

class CabinCreateView(CreateView):
      fields = '__all__'
      model = Cabin
      success_url = reverse_lazy("NewApp:logindex")

  In cabin templates



  <div class="row">
                                        <div class="col-md-6">
                                            <form method="POST">
                                             {% csrf_token %}
                                                {{ form.non_field_errors }}

                                                <div class="col col-md-12">
                                                <div class="fieldWrapper" >
                                                {{ form.centre_name.errors }}
                                                    <div class="form-group col col-md-3">
                                                        <label>Centre Name</label>
                                                        {{form.centre_name}}
                                                </div>

                                                <div class="form-group col col-md-3" style="float:right; margin-top=-80px;width=200%">
                                                    <label for="{{form.code.id_for_label}" style="margin-left:200px;width:200%;white-space:nowrap;">Code</label>
                                                     <input type="text" placeholder="Code" value="{{form.code.value}}" name="code" maxlength="6" id="id_code" style="width:500px; margin-left:200px;">
                                                </div>

                                                </div>

                                                <div class="col col-md-12">
                                                 <div class="form-group col col-md-3" style="float:right; margin-top=-80px;">
                                                <label for="{{form.total_seats.id_for_label}" style="margin-left:200px;width:200px;white-space:nowrap;">seats</label>
                                                 <input type="text" placeholder="seats" name="total_seats" id="id_total_seats" style="width:500px; margin-left:200px;">
                                                 </div>

                                                    <div class="fieldWrapper" >
                                                {{ form.category.errors }}

                                                 <div class="form-group col col-md-3" >

                                                    <label for="{{form.category.id_for_label}" style="margin-top:-40px">Category</label>
                                                <input type="text" name="category"  maxlength="100" id="id_category" placeholder="Category" style="width:500px";>
                                                 </div></div></div>

                                                <div class="col col-md-12">


                                                    <div class="fieldWrapper" >
                                                {{ form.released_date.errors }}

                                                 <div class="form-group col col-md-3" style="float:right; margin-top=-80px;">
                                                <label for="{{form.released_date.id_for_label}" style="margin-left:200px;width:200px;white-space:nowrap;">released date</label>
                                                 <input type="text" placeholder="%mm%dd%yyyy" name="released_date" id="id_released_date" style="width:500px; margin-left:200px;">
                                                 </div>
                                                    </div>

                                                <div class="fieldWrapper" >
                                                {{ form.booked_date.errors }}

                                                 <div class="form-group col col-md-3" >
                                                    <label for="{{form.booked_date.id_for_label}" style="margin-top:-40px">booked date</label>
                                                <input type="text" name="booked_date"   id="id_booked_date"  placeholder="%mm%dd%yyyy" style="width:500px";>
                                                 </div>
                                                    </div>

                                                 </div>


                                        <div class="col col-md-12">


                                        <div class="form-group col col-md-3" >
                                                    <label for="{{form.price.id_for_label}" style="margin-top:-40px">price</label>
                                                <input type="text" name="price"  maxlength="10" id="id_price" placeholder="in rupees" style="width:500px";>
                                                 </div>
                                            </div>
                                                <div class="form-group col col-md-3" >

                                                <input type="submit"  onclick="comparedate()" value="Save" class="btn btn-primary" style=" height:30px;width:80px;padding-bottom:2em;"/>
                                                </div></div>

                                                </form>
models.py中的

班级中心(模型.模型):
name=models.CharField(max_length=50,blank=False,unique=True)
地址=models.CharField(最大长度=250)
phone_regex=RegexValidator(regex=r'^\+-1?\d{9,15}$',
message=“必须以“+9999999”格式输入电话号码。最多允许输入10位数字。”)
contact=models.CharField(最大长度=100,空白=False)
phone=models.CharField(验证器=[phone_regex],max_length=10,blank=True)#验证器应该是一个列表
slug=models.SlugField(unique=False)
舱室等级(型号.型号):
random_string=str(random.randint(100000,999999))
centre\u name=models.ForeignKey(中心,on\u delete=models.CASCADE)
code=models.CharField(最大长度=6,空白=False,唯一=True,默认=random\u字符串)
总座位数=models.IntegerField(blank='False')
views.py
类CAbIncremateView(CreateView):
字段='\uuuu所有\uuuu'
型号=座舱
success\u url=reverse\u lazy(“NewApp:logindex”)
舱内模板
{%csrf_令牌%}
{{form.non_field_errors}}
{{form.centre_name.errors}
中心名称
{{form.centre_name}
密码
座位
{{form.category.errors}
类别
{{form.released_date.errors}}
发布日期
{{form.booked_date.errors}
预定日期
价格

“此字段是必需的”

导致此错误的字段添加: 空白=真 在里面
或者添加并定义一些默认值。

我在中心名称和客舱代码中添加了blank=True,但我的表单仍然没有提交。它在centre_name中显示“字段是必需的”,并限制我的表单被提交。如果在name属性上设置为blank=True,则还必须设置null=True,因为您具有unique=True。我的表单仍然没有被提交。