在模型中使用子弹后,表单未提交

时间:2019-07-05 13:34:44

标签: python django django-models django-forms slug

我正在一个项目中,我需要在URL中隐藏主键,因此我在所有模型中都实现了Slug。但是使用外键到另一个模型的模型似乎无法正常工作。该表单具有价值,但是无论我做什么都不会提交。我不知道我的代码有什么问题。

views.py

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


    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.cabin =  Cabin.objects.filter(slug=self.kwargs['slug'])[0]
        self.object.save()
        return HttpResponseRedirect(self.get_success_url())

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)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Centre, self).save(*args, **kwargs)

   def __str__(self):
        return self.name

   def get_absolute_url(self):
        return reverse("index")


 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')
    category=models.CharField(max_length=100, default=False)
    slug = models.SlugField(unique=False,default=None)

def save(self, *args, **kwargs):
    self.slug = slugify(self.category)
    super(Cabin, self).save(*args, **kwargs)



def __str__(self):
    return self.code

def get_absolute_url(self):
    return reverse("index")

模板

 <form method="POST">
  {% csrf_token %}
   <div class="form-group col col-md-3">
   <label>Centre Name</label>
   {{form.centre_name}}
   </div>
  <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;">


 <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;">


  <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";>



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

  </form>

中心表格工作正常,但是按提交后,机舱表格未提交。该页面正在重新加载,这意味着该表单无效。也没有任何错误出现。我没有在每个字段中都发布{{form.errors}},因为代码太多了。我已经将其应用于所有领域。

0 个答案:

没有答案