“我的模板”分为两列。 我只有一个模型,但目的是将表单分为两部分,第一列中的一部分,第二列中的另一部分。我旨在将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">
答案 0 :(得分:1)
您不能有两个$dateString = '10.12.2018'
# DateTime.Parse accepts an IFormatProvider - and CultureInfo implements IFormatProvider!
# Let's take French
$frenchCulture = [cultureinfo]::GetCultureInfo('fr-FR')
# Pass the format provider as the second argument
[datetime]::Parse($dateString, $frenchCulture)
方法,并且实际上并不需要它。您可以借助FormHelper()将这两个“列”放在两个单独的__init__
标记内。
<div>
答案 1 :(得分:0)
这似乎暂时可行(主要受Define crispy forms context names for two forms in one启发,但在这里我基于ModelForm创建了两个新表单):我希望我自己能更好地理解该解决方案,但至少可以按预期工作。
# 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 %}