My Form is not showing errors and input values after validation

497 views Asked by At

My model.py

class VehicleInquiry(TimeStampedModel):
    inquiry_status = models.PositiveSmallIntegerField(_("inquiry status"), choices=INQUIRY_STATUS_CHOICES, default=1)
    full_name = models.CharField(_("full name"), max_length=100)
    address = models.CharField(_("address"), max_length=200)
    ---- other fields ----

My Crispy form.py:

class VehicleInquiryForm(forms.ModelForm):
    --- overridden form fields ---    

class Meta:
    model = VehicleInquiry
    fields = ('full_name', 'email')


def __init__(self, request=None, stock_product=None, *args, **kwargs):
    super(VehicleInquiryForm, self).__init__(*args, **kwargs)
    self.fields['is_subscribed'].label = _("Keep me updated")
    self.helper = FormHelper()
    self.helper.template_pack = "bootstrap3"
    self.helper.form_method = "post"
    self.helper.form_id = "vehicle-shipping-form"
    self.initial['insurance'] = True
    self.helper.add_input(
        Submit('inquiry', _('Inquiry'), css_class='btn btn-default',)
    )
    self.helper.form_method = 'post'
    self.helper.layout = Layout(
        Fieldset(
        _("1. Choose Your Final Destination"),
        Div(
            Field('country2', css_class="order-select-country"),
        ),
        
            --- other fields ---                   
        )

My CBV view.py:

class VehicleStockDetailView(TemplateView):
    model = StockProduct
    template_name = "site/product/vehicle-detail.html"
    template_name_done = "site/contact/contact-us-done.html"
    template_name_done_email = "site/contact/emails/contact-us-done.html"
    
    def post(self, request, slug, *args, **kwargs):
        form = VehicleInquiryForm(request.POST)
        ip=""
        if form.is_valid():             
            form.save()
            return render(request, self.template_name_done, {
                'full_name': request.POST['full_name'],
                'email': request.POST['email'],
            })
    
    
        vehicle = get_object_or_404(VehicleStock, slug=slug)
        return render(request, self.template_name, {
            'product': vehicle,
            'form': form
        })
  
    def get(self, request, slug, *args, **kwargs):
        vehicle = get_object_or_404(VehicleStock, slug=slug)
        form = VehicleInquiryForm()
        return render(request, self.template_name, {
            'product': vehicle,
            'form': form
        })

The form is loading OK as a GET, but when I click Inquiry as a post request it does not show neither errors nor entered values. In my template I am loading form as {% crispy form %}. Need help.

1

There are 1 answers

2
Daniel Roseman On BEST ANSWER

You've changed the signature of the form init function so that the first positional arguments are request and stock_product. You should avoid doing this - generally I'd recommend using kwargs instead, but in this case you should remove them completely as you are not using those values in that method.

Also note your code would be simpler if you used a FormView, which takes care of almost all of that logic for you.