我正在制作一个Python / Flask应用程序(作为我学习过程的一部分),要求用户在注册时进行选项选择。
我从db生成的选项运行正常,但是我在jinja模板中键入的选项返回:“ ValueError:没有足够的值要解压(预期2,得到1)” 用以下代码填充表单:
form.primary_business_type.choices = request.form.get("PrimaryBusinessType")
当我使用以下代码填充表单时,和“不是有效选择” 对于单个SelectField,而“此字段是必需的” 对于SelectMultipleField:
form.primary_business_type.choices = \
[(request.form.get("PrimaryBusinessType"), request.form.get("PrimaryBusinessType"))]
请对此提供任何帮助。
以下是错误的屏幕截图: Actual errors on form
下面还有一些代码可以更清楚:
forms.py
class CreateAccountWithPhoneForm(FlaskForm):
.......
primary_business_type = SelectField('BUSINESS TYPE:', validators=[DataRequired()])
primary_business_sub_type = SelectField('Sub-Type:', validators=[DataRequired()])
major_brands_or_models = SelectMultipleField('Major Brand/Model:', validators=[DataRequired()])
additional_business_types = SelectMultipleField('Additional Business Types:', validators=[Optional()])
business_categories = SelectMultipleField('BUSINESS CATEGORY:', validators=[DataRequired()])
....
submit = SubmitField('Create Account')
并且,模板中的实际选项(示例):
<!-- PrimaryBusinessType -->
{{ form.primary_business_type.label(class="form-control-label mr-2") }}
{% if form.primary_business_type.errors %}
{{ form.primary_business_type(class="form-control form-control_lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.primary_business_type.errors %}
<span> {{ error }}</span>
{% endfor %}
</div>
{% else %}
<select name="PrimaryBusinessType">
<option class="text-secondary">Select...</option>
<option value="AutoDealer">Automobiles</option>
<option value="Clothing">Clothing</option>
<option value="Beverages">Beverages</option>
<option value="Other">Other</option>
</select>
{% endif %}
</div>
<div class="form-group">
<!-- PrimaryBusinessSubType -->
{{ form.primary_business_sub_type.label(class="form-control-label mr-2") }}
{% if form.primary_business_sub_type.errors %}
{{ form.primary_business_sub_type(class="form-control form-control_lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.primary_business_sub_type.errors %}
<span> {{ error }}</span>
{% endfor %}
</div>
{% else %}
<select name="PrimaryBusinessSubType">
<option class="AutoDealer" value="MotorcycleSeller">Motorcycle Seller</option>
<option class="AutoDealer" value="3WheelerSeller">3 Wheeler Seller</option>
<option class="AutoDealer" value="CarSeller">Car Seller</option>
<option class="Clothing" value="Laces">Laces</option>
<option class="Clothing" value="SportsWears">Sports Wears</option>
</select>
{% endif %}
</div>
<div class="form-group">
<!-- MajorBrandOrModel -->
{{ form.major_brands_or_models.label(class="form-control-label mr-2") }}
{% if form.major_brands_or_models.errors %}
{{ form.major_brands_or_models(class="form-control form-control_lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.major_brands_or_models.errors %}
<span> {{ error }}</span>
{% endfor %}
</div>
{% else %}
<select name="MajorBrandOrModel">
<option class="CarSeller" value="Mercedese">Mercedese</option>
<option class="SportsWears" value="Puma">Puma</option>
</select>
{% endif %}
</div>
<div class="form-group">
<!-- AdditionalBusinessTypes-->
{{ form.additional_business_types.label(class="form-control-label mr-2") }}
{% if form.additional_business_types.errors %}
{{ form.additional_business_types(class="form-control form-control_lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.additional_business_types.errors %}
<span> {{ error }}</span>
{% endfor %}
</div>
{% else %}
<select name="AdditionalBusinessTypes" class="selectpicker" multiple data-show-subtext="true" data-live-search="true">
<optgroup label="Auto Dealer">
<option class="AutoDealer" value="MotorcycleSeller">Motorcycle Seller</option>
<option class="AutoDealer" value="3WheelerSeller">3 Wheeler Seller</option>
<option class="AutoDealer" value="CarSeller">Car Seller</option>
</optgroup>
<optgroup label="Cloathing">
<option class="Cloathing" value="Laces">Laces</option>
<option class="Cloathing" value="SportsWears">Sports Wears</option>
<option class="Cloathing" value="TShirts">T Shirts</option>
</optgroup>
</select>
{% endif %}
</div>
<div class="form-group">
<!-- BusinessCategories-->
{{ form.business_categories.label(class="form-control-label mr-2") }} <br>
{% if form.business_categories.errors %}
{{ form.business_categories(class="form-control form-control_lg is-invalid") }}
<div class="invalid-feedback">
{% for error in form.business_categories.errors %}
<span> {{ error }}</span>
{% endfor %}
</div>
{% else %}
{% for category in business_categories %}
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input" name="BusinessCategories" value="{{ category }}">{{ category }}
</label>
</div>
{% endfor %}
{% endif %}
</div>
然后,在视图中填充表单字段的地方,我认为这是错误的:
使用下面的代码将返回错误:“无效选择” (对于单个SelectField)和“此字段为必填” (对于SelectMultipleField)
@users.route("/create-account-with-phone", methods=['GET', 'POST'])
def create_account_with_phone():
if current_user.is_authenticated:
return redirect(url_for('main.home'))
form = forms.CreateAccountWithPhoneForm()
form.primary_business_type.choices = \
[(request.form.get("PrimaryBusinessType"), request.form.get("PrimaryBusinessType"))]
form.primary_business_sub_type.choices = \
[(request.form.get("PrimaryBusinessSubType"), request.form.get("PrimaryBusinessSubType"))]
form.major_brands_or_models.choices = \
[(request.form.get("MajorBrandOrModel"), request.form.get("MajorBrandOrModel"))]
form.additional_business_types.choices = \
[(request.form.get("AdditionalBusinessTypes"), request.form.get("AdditionalBusinessTypes"))]
form.business_categories.choices = \
[(request.form.get("BusinessCategories"), request.form.get("BusinessCategories"))]
同时,以下内容返回:“ ValueError:没有足够的值要解压(预期2,得到1)”
@users.route("/create-account-with-phone", methods=['GET', 'POST'])
def create_account_with_phone():
if current_user.is_authenticated:
return redirect(url_for('main.home'))
form = forms.CreateAccountWithPhoneForm()
form.primary_business_type.choices = request.form.get("PrimaryBusinessType")
form.primary_business_sub_type.choices = request.form.get("PrimaryBusinessSubType")
form.primary_business_sub_type.choices = request.form.get("MajorBrandOrModel")
form.primary_business_sub_type.choices = request.form.get("AdditionalBusinessTypes")
form.primary_business_sub_type.choices = request.form.get("BusinessCategories")
再次感谢您的任何帮助。 预先感谢