[英]Submitting 2 custom Crispy Forms with 1 button Django
I have 2 forms with the second form referencing the first form.我有 2 forms,第二种形式引用第一种形式。 I can get the forms to execute correctly using crispy forms, however, when I customize each form using helper = FormHelper() they will no longer submit together.我可以使用 crispy forms 使 forms 正确执行,但是,当我使用 helper = FormHelper() 自定义每个表单时,它们将不再一起提交。 Essentially one form is submitted and the other form thinks it is missing all of its input data.基本上提交了一个表单,而另一个表单认为它丢失了所有输入数据。
Forms.py Forms.py
from django.forms import ModelForm, forms
from .models import Item, Item_dimensions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column
# Create the form class.
class List_Item_Form(ModelForm):
class Meta:
model = Item
exclude = ('id','creator','created_on','updated_on' )
helper = FormHelper()
helper.layout = Layout(
Row(
Column('item_name', css_class='form-group col-md-6 mb-0'),
Column('price', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
'short_description',
'description',
Row(
Column('main_image', css_class='form-group col-md-2.5 mb-0'),
Column('image_2', css_class='form-group col-md-2.5 mb-0'),
Column('image_3', css_class='form-group col-md-2.5 mb-0'),
Column('image_4', css_class='form-group col-md-2.5 mb-0'),
Column('image_5', css_class='form-group col-md-2.5 mb-0'),
css_class='form-row'
),
'quantity'
)
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
helper.form_method = 'POST'
class List_Item_Dimensions(ModelForm):
class Meta:
model = Item_dimensions
exclude = ('id','item_id')
helper = FormHelper()
helper.layout = Layout(
Row(
Column('x_dim_mm', css_class='form-group col-md-4 mb-0'),
Column('y_dim_mm', css_class='form-group col-md-4 mb-0'),
Column('z_dim_mm', css_class='form-group col-md-4 mb-0'),
css_class='form-row'
),
'weight_grams',
Submit('submit', 'add_listing')
)
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
helper.form_method = 'POST'
I have tried removing我试过删除
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary')) helper.add_input(提交('提交', '提交', css_class='btn-primary'))
and using just one submit button but it didn't work并且只使用一个提交按钮但它不起作用
Views.py视图.py
@login_required
def AddListing(request):
if request.method == 'POST':
form = List_Item_Form(request.POST,request.FILES)
dim_form = List_Item_Dimensions(request.POST)
if form.is_valid() and dim_form.is_valid():
itemName = form.cleaned_data['item_name']
price = form.cleaned_data['price']
s_desc = form.cleaned_data['short_description']
desc = form.cleaned_data['description']
quantity = form.cleaned_data['quantity']
main_img = form.cleaned_data['main_image']
image_2 = form.cleaned_data['image_2']
image_3 = form.cleaned_data['image_3']
image_4 = form.cleaned_data['image_4']
image_5 = form.cleaned_data['image_5']
x_dim = dim_form.cleaned_data['x_dim_mm']
y_dim = dim_form.cleaned_data['y_dim_mm']
z_dim = dim_form.cleaned_data['z_dim_mm']
weight = dim_form.cleaned_data['weight_grams']
current_user = request.user
model_instance = Item(creator=current_user, item_name=itemName,
price=price,short_description=s_desc,
description=desc, quantity=quantity,
main_image=main_img, image_2=image_2,
image_3=image_3, image_4 = image_4,
image_5=image_5)
dim_instance = Item_dimensions(x_dim_mm = x_dim,
y_dim_mm =y_dim,
z_dim_mm =z_dim,
weight_grams = weight)
model_instance.save()
# dim_instance.save(commit=False)
dim_instance.item_id = model_instance
dim_instance.save()
return HttpResponseRedirect('/user_profile/items/')
else:
form = List_Item_Form()
dim_form = List_Item_Dimensions()
return render(request, 'store/add_listing.html', {'form': form,'dim_form':dim_form})
template.html模板.html
{% block content %}
<div>
<h1> list a new item </h1>
{% load crispy_forms_tags %}
{% csrf_token %}
<form method="post", enctype="multipart/form-data">{% csrf_token %}
{% crispy form %}
{% crispy dim_form %}
{% comment %} {{ form|crispy }}
{{ dim_form|crispy }} {% endcomment %}
<input name="submit" type="post" value="template button">
</form>
</div>
{% endblock content %}
{% crispy form %}
automatically adds <form></form>
tags unless otherwise you specify form_tag = False
. {% crispy form %}
自动添加<form></form>
标签,除非您另外指定form_tag = False
。 See https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html参见https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html
Here you've already given an outer tag and hence the crispy
tags inside it will create nested forms. This makes your HTML invalid.在这里你已经给出了一个外部标签,因此它里面的crispy
标签将创建嵌套的 forms。这使得你的 HTML 无效。 Check https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#crispy-tag-forms检查https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#crispy-tag-forms
PS: (Haven't used django recently) - don't you need to include the helper and other customisations inside the constructor? PS:(最近没用过 django)- 你不需要在构造函数中包含 helper 和其他定制吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.