programmer
programmer

Reputation: 11

django form not submitting due to slug and foreign key

My django cabin form is not submitting. I have applied foreign key and slug in my cabin's model and after that my form stopped getting submitted. whenever I enter all the fields and hit submit button,the form page is getting reloaded and no data is getting submitted.Most Importantly it is not showing any error so that I can fix it.I have tried and searched a lot about it and tried different things but still it didn't work. I am stuck to it. Please help!!

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)


  class Cabin(models.Model):
     random_string = str(random.randint(100000, 999999))
     centre_name = models.ForeignKey(Centre, on_delete=models.CASCADE,blank=True,null=True)
     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)
     booked_date=models.DateField(blank='False')
     released_date=models.DateField(blank='False')
     price=models.IntegerField(blank=False, default=None)
     slug = models.SlugField(unique=False,default=None)

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


 In views.py file

 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())


   In my cabin template,

   <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="%yyyy-mm-dd" 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="%yyyy-mm-dd" 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>


                                        </div></div></div></div></div></div></div></div></div></div>

Upvotes: 0

Views: 221

Answers (1)

Jacinator
Jacinator

Reputation: 1413

It looks like your problem is in your views.py file. You are telling the view that it should build a form including __all__ of the fields on the model, but in your template you don't include the slug field. Since you have overridden the save method on the Cabin model to populate the slug field, I guess you don't want that displayed in the template.

You have two options for fixing this. You could add a hidden field to the template containing the slug field. Or, what I think is a better option, you can change the fields attribute on your view to exclude the slug field.

 class CabinCreateView(CreateView):
     fields = ('centre_name', 'code', 'total_seats', 'category', 'booked_date', 'released_date', 'price',)
     model = Cabin
     success_url = reverse_lazy("NewApp:logindex")

P.S. This isn't a problem you are asking about, but I couldn't help noticing that you have what looks like multiple broken variables in your template. You might want to check if your label for="" attributes are working as expected.

Upvotes: 1

Related Questions