【问题标题】:My Form is not showing errors and input values after validation验证后我的表单未显示错误和输入值
【发布时间】:2017-05-12 14:32:16
【问题描述】:

我的模型.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 ----

我的 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 ---                   
        )

我的 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 加载 OK,但是当我单击 Inquiry 作为发布请求时,它既不显示错误也不显示输入的值。在我的模板中,我将表单加载为 {% crispy form %}。需要帮助。

【问题讨论】:

  • 为什么不为此使用表单视图?
  • 因为我是从另一个使用 TemplateView 的视图 BaseView 扩展而来的。在基本视图中,我正在定义元文件。这就是为什么我不喜欢将所有视图更改为 FormView

标签: django django-forms django-templates django-crispy-forms


【解决方案1】:

您已更改表单 init 函数的签名,以便第一个位置参数是 request 和 stock_product。您应该避免这样做 - 通常我建议改用 kwargs,但在这种情况下,您应该完全删除它们,因为您在该方法中不使用这些值

另外请注意,如果您使用 FormView,您的代码会更简单,它会为您处理几乎所有的逻辑。

【讨论】:

  • 谢谢你,我经常使用stock_product。但是为了简化代码,我删除了一些对问题不重要的字段。无论如何,我会听从您的建议并尝试使用 FormView。谢谢。我稍后会公布结果。
  • 按照您的建议,我从 init 中删除了 request 和 stock_product。现在它起作用了。我从现在开始使用 kwrgs。谢谢你节省了我的时间,我被困在这里并阅读了很多文章。但主要是关于 FBV 或 FormView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
相关资源
最近更新 更多