为什么我的WTForms-JSON表单无法正确更新? [英] Why doesn't my WTForms-JSON form update correctly?

查看:133
本文介绍了为什么我的WTForms-JSON表单无法正确更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个读取JSON的网页,并使用WTForms-JSON将其呈现为表单.当我提交表单时,form.data不会更新.为什么这行不通?

I am developing a web page that reads JSON and presents it in a form using WTForms-JSON. When I submit the form, form.data isn't updated. Why isn't this working?

views.py:

from flask import render_template, flash, redirect, request
from app import app
from .forms import PolicyForm
import json
import urllib2

@app.route('/policy', methods=['GET', 'POST'])
def policy():
    url = 'http://dcdemoappsrv1:8081/direct/policy?policyNumber=000000005&everything=true&discounts=true&coverages=true&vehicles=true&nonDescribedVehicle=true&applicant=true&drivers=true&namedInsureds=true&additionalListedInsureds=true'
    response = urllib2.urlopen(url).read()
    pol_json = json.loads(response) 

    form = PolicyForm.from_json(pol_json)

    if form.validate_on_submit():
        flash('data=%s' % str(form.data))
        flash('form pol no =%s' % str(form.policyNumber.data))
        return redirect('/index')

    flash('inital data=%s' % str(form.data))
    return render_template('policy.html',  title='Policy Form', form=form)

forms.py:

from wtforms import Form
from wtforms.fields import BooleanField, StringField, TextField, FloatField, FormField, IntegerField, DateField, SubmitField
from wtforms.validators import DataRequired, InputRequired

class Address(Form):
    street = TextField('Street', validators=[InputRequired()])
    street2 = TextField('Street2')
    city = TextField('City', validators=[InputRequired()])
    state = TextField('State', validators=[InputRequired()])
    zip = TextField('Zip', validators=[InputRequired()])
    county = TextField('County', validators=[InputRequired()])
    latitude = FloatField('Latitude')
    longitude = FloatField('Longitude')
    id = StringField('ID')

class Applicant(Form):
    firstName = TextField('First Name', validators=[InputRequired()])
    lastName = TextField('Last Name', validators=[InputRequired()])
    birthDate = DateField('Birth Date', validators=[InputRequired()])
    age = IntegerField('Age', validators=[InputRequired()])
    id = StringField('ID')

class PolicyForm(Form):
    policyNumber = TextField('Policy Number')
    applicant = FormField(Applicant, label='Applicant')
    address = FormField(Address, label='Address')

<!-- extend from base layout -->
{% extends "base.html" %}

{% block content %}
  <h1>Policy</h1>
  {%  import "__formhelpers.html" as forms %}
<form action="/policy" method="POST" name='policy'>
{{ forms.render(form) }}
<p><input type="Submit" value="Update Policy"></p>
</form>
{% endblock %}

formhelpers.html:

{% macro render(form) %}
<dl>
{% for field in form if field.type not in ["HiddenField", "CSRFTokenField"] %}
    <dt>{{ field.label }} </dt>
    <dd>{{ field }}
    {% if field.errors %}
        <ul class="errors">
        {% for error in field.errors %}
            <li>{{error}}</li>
        {% endfor %}
        </ul>
    {% endif %}</dd>
{% endfor %}
</dl>
{{ form.hidden_tag() }}
{% endmacro %}

推荐答案

您始终使用远程JSON数据填充表单.您只应在首次提交表单时执行此操作,而不是在处理提交的数据时执行此操作.

You are always populating the form with the remote JSON data. You should only do this when first presenting the form, not when processing the submitted data.

# ...
if not form.is_submitted():
    data = json.load(urllib2.urlopen(url))
    form = PolicyForm.from_json(data)
else:
    form = PolicyForm()  # will populate from submitted data

if form.validate_on_submit():
# ...

这篇关于为什么我的WTForms-JSON表单无法正确更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆