Python Crispy Forms:FormHelper用于拆分表单/两列相同的模型
我的模板分为两列。 我只有一个模型,但目标是将一个表单分成两部分,一部分在第一列,另一部分在第二列。我的目标是使用FormHelper制作酥脆的表单 可用的文档给出了一个隐晦的提示,但没有任何示例,这样的解释有点不够 使用助手呈现多个表单 我们经常被问到:“如何呈现两个或多个表单,以及它们的 各自的助手,使用{%crispy%}标记,没有标记 渲染两次?简单,您需要将form_tag helper属性设置为 在每个助手中都为False:Python Crispy Forms:FormHelper用于拆分表单/两列相同的模型,python,django,django-forms,django-crispy-forms,Python,Django,Django Forms,Django Crispy Forms,我的模板分为两列。 我只有一个模型,但目标是将一个表单分成两部分,一部分在第一列,另一部分在第二列。我的目标是使用FormHelper制作酥脆的表单 可用的文档给出了一个隐晦的提示,但没有任何示例,这样的解释有点不够 使用助手呈现多个表单 我们经常被问到:“如何呈现两个或多个表单,以及它们的 各自的助手,使用{%crispy%}标记,没有标记 渲染两次?简单,您需要将form_tag helper属性设置为 在每个助手中都为False: self.helper.form_tag = False
self.helper.form_tag = False
然后,您将不得不围绕
表格:
你不能有两个
\uuuu init\uuuu
方法,实际上你并不需要它。在FormHelper()的帮助下,您可以将这两个“列”括在两个单独的
标记中
希望,这给了你窍门。请参阅更多信息。这似乎暂时有效(主要是受模型表单的启发,但我在这里创建了两个基于模型表单的新表单):我希望自己能够更好地完全理解解决方案,但至少它能按预期工作
# 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__')
class CarForm(ProductForm):
def __init__(self, *args, **kwargs):
super().__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_tag = False
self.helper.add_input(Submit('submit', 'Submit'))
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),
)
class TransactionForm(ProductForm):
def __init__(self, *args, **kwargs):
super().__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_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 = CarForm
model = Product
def get_context_data(self, **kwargs):
context = super(ProductFormView, self).get_context_data(**kwargs)
context['form_2'] = TransactionForm(instance=self.model())
return context
def form_valid(self, form):
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
return self.render_to_response(
self.get_context_data(
form=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 %}
{% crispy form_2 %}
</form>
{% endblock col4_content %}
#models.py
类别产品(型号.型号):
car\u model\u make=models.CharField(默认值=B',max\u length=1,blank=True,choices=car\u TYPE)
状态为二手=型号。布尔字段(空白=真)
seller=models.CharField(最大长度=50,空白=True,选项=seller\u类型)
买方=型号.CharField(最大长度=50,空白=真,选项=买方类型)
#forms.py
类别ProductForm(ModelForm):
类元:
型号=产品
字段=(''所有''
类别表格(产品表格):
定义初始化(self,*args,**kwargs):
super()
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_标记=False
self.helper.add_输入(Submit('Submit','Submit'))
型号='汽车型号'
二手车='状态为二手车'
self.fields[model].label=“model”
self.fields[secondhand].label=“是二手的”
self.helper.layout=布局(
字段(模型),
现场(二手),
)
类事务处理表单(ProductForm):
定义初始化(self,*args,**kwargs):
super()
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_标记=False
卖方=‘卖方’
买方=‘买方’
self.fields[seller].label=“seller”
self.fields[buyer].label=“buyer”
self.helper.layout=布局(
现场(卖方),
现场(买方),
)
#views.py
类ProductFormView(FormView):
表格\类别=表格
型号=产品
def获取上下文数据(自身,**kwargs):
context=super(ProductFormView,self)。获取上下文数据(**kwargs)
上下文['form_2']=TransactionForm(实例=self.model())
返回上下文
def表单_有效(自身、表单):
self.object=form.save()
返回HttpResponseRedirect(self.get_success_url())
def表单_无效(自身、表单):
返回self.render\u to\u响应(
self.get\u上下文\u数据(
形式=形式,
)
)
def get_success_url(自我):
返回反向('索引')
#url.py
URL模式=[
路径(“”,索引,name='index'),
路径('product/',ProductFormView.as_视图(template_name='product/product.html'),
#html模板
{%extends“product/base.html”%}
{%load crispy_forms_tags%}
{%block col8_content%}
{%csrf_令牌%}
{%crispy form%}
{%endblock col8_content%}
{%block col4_content%}
{%crispy form_2%}
{%endblock col4_content%}
谢谢,根据您的回答,我已经更新了我的问题。同时,它将按照您的建议工作,但不幸的是,在我的情况下,实际布局要复杂得多,我需要一个“块”但是,如果这是唯一的解决方案,我需要将所有的格式放在div中。我可以知道表单中那些块和endblock的用途吗?上面的块指的是“插入表单对象,例如{%crispy form%}”(sry表示混淆的术语)。在模板中使用块的想法是因为我删除了标记“self.helper.form_tag=False”,在我的第二列中有一个手动表单,这是我保存两个表单的方式。因此,块col8是我的左列,块col4是我的右列。在每个块中都有一个表单,两个表单来自同一个模型,我需要保存它们同时出现。对不起,我不明白。你能用你的区块标签的内容更新帖子吗?sry由于理解错误,添加了内容,本质上是一个折叠卡片列表,每个都包含表单字段(来自同一型号)
# 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">
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'
)
)
# 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__')
class CarForm(ProductForm):
def __init__(self, *args, **kwargs):
super().__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_tag = False
self.helper.add_input(Submit('submit', 'Submit'))
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),
)
class TransactionForm(ProductForm):
def __init__(self, *args, **kwargs):
super().__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_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 = CarForm
model = Product
def get_context_data(self, **kwargs):
context = super(ProductFormView, self).get_context_data(**kwargs)
context['form_2'] = TransactionForm(instance=self.model())
return context
def form_valid(self, form):
self.object = form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
return self.render_to_response(
self.get_context_data(
form=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 %}
{% crispy form_2 %}
</form>
{% endblock col4_content %}