我的表单在验证后未显示错误和输入值
My Form is not showing errors and input values after validation
我的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 ----
我的脆皮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 ---
)
我的 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
})
表单作为 GET 加载正常,但是当我作为 post 请求单击“查询”时,它既不显示错误也不显示输入的值。在我的模板中,我将表单加载为 {% crispy form %}。需要帮助。
您已经更改了表单初始化函数的签名,因此第一个位置参数是请求和 stock_product
。您应该避免这样做 - 通常我建议改用 kwargs
,但在这种情况下,您应该完全删除它们,因为您 未在该方法中使用这些值 。
另请注意,如果您使用 FormView,您的代码会更简单,它会为您处理几乎所有的逻辑。
我的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 ----
我的脆皮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 ---
)
我的 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
})
表单作为 GET 加载正常,但是当我作为 post 请求单击“查询”时,它既不显示错误也不显示输入的值。在我的模板中,我将表单加载为 {% crispy form %}。需要帮助。
您已经更改了表单初始化函数的签名,因此第一个位置参数是请求和 stock_product
。您应该避免这样做 - 通常我建议改用 kwargs
,但在这种情况下,您应该完全删除它们,因为您 未在该方法中使用这些值 。
另请注意,如果您使用 FormView,您的代码会更简单,它会为您处理几乎所有的逻辑。