我的模板分为两列。
我只有一个模型,但目的是将表单分为两部分,第一列中的一部分,第二列中的另一部分。我旨在将FormHelper用于脆皮表单。
可用文档给出了一个模糊的提示,但没有任何示例,这种解释的尝试就显得有些不足。
https://django-crispy-forms.readthedocs.io/en/d-0/tags.html#rendering-several-forms-with-helpers
用助手渲染几种形式
通常我们会被问到:“如何渲染两个或多个表单,以及它们的
各自的助手,使用{%crispy%}标签,但没有标签
渲染两次?”容易,您需要将form_tag helper属性设置为
在每个助手中都是错误的:
self.helper.form_tag = False
然后,您将不得不编写一些围绕
形式:
<form action="{% url submit_survey %}" class="uniForm" method="post">
{% crispy first_form %}
{% crispy second_form %}
</form>
更新:这篇文章解释了Crispy文档的通行证
Define crispy forms context names for two forms in one
下面是我的代码。两个FormHelper将模型分为两部分,第一部分包含字段:
['car_model_make', 'status_is_secondhand']
具有字段的第二部分:
['seller', 'buyer']
我一直在寻找一种“调用”特定
{% crispy form %}
的方法。给定“帮助”文档”,它们看起来像是{% crispy product-modelform_1 %}
和{% crispy product-modelform_2 %}
,它们不起作用。# models.py
class Product(models.Models):
car_model_make = models.CharField(default='B', max_length=1, blank=True, choices=CAR_TYPE)
status_is_secondhand = models.BooleanField(blank=True)
seller = models.CharField(max_length=50, blank=True, choices=SELLER_TYPE)
buyer = models.CharField(max_length=50, blank=True, choices=BUYER_TYPE)
# forms.py
class ProductForm(ModelForm):
class Meta:
model = Product
fields = ('__all__')
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-4'
self.helper.field_class = 'col-sm-8'
self.helper.form_id = 'product-modelform'
self.helper.form_tag = False
model = 'car_model_make'
secondhand = 'status_is_secondhand'
self.fields[model].label = "Model"
self.fields[secondhand].label = "Is Secondhand"
self.helper.layout = Layout(
Field(model),
Field(secondhand),
)
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-4'
self.helper.field_class = 'col-sm-8'
self.helper.form_id = 'product-modelform'
self.helper.form_tag = False
seller = 'seller'
buyer = 'buyer'
self.fields[seller].label = "Seller"
self.fields[buyer].label = "buyer"
self.helper.layout = Layout(
Field(seller),
Field(buyer),
)
# views.py
class ProductFormView(FormView):
form_class = ProductForm
def form_valid(self, form):
form.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('index')
# urls.py
urlpatterns = [
path('', index, name='index'),
path('product/', ProductFormView.as_view(template_name='product/product.html'),
# html template
{% extends "product/base.html" %}
{% load crispy_forms_tags %}
{% block col8_content %}
<form id="product-modelform" method="post">
{% csrf_token %}
{% crispy form %}
{% endblock col8_content %}
{% block col4_content %}
</form>
{% endblock col4_content %}
<input type="submit" value="Submit">
最佳答案
您不能有两个__init__
方法,并且实际上并不需要它。您可以借助FormHelper()将这两个“列”放在两个单独的<div>
标记内。
def __init__(self, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-sm-4'
self.helper.field_class = 'col-sm-8'
self.helper.form_id = 'product-modelform'
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
Div('car_model_make','status_is_secondhand', css_class='col-lg-6 col-md-6 col-sm-12'),
Div('seller','buyer', css_class='col-lg-6 col-md-6 col-sm-12'),
css_class='row'
)
)
希望,这给您带来了窍门。有关更多信息,请参考Layouts。
关于python - 脆皮表单:FormHelper用于拆分表单/两列使用同一模型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55023366/