【问题标题】:Dropdown box in Flask App using WTForms not updating使用 WTForms 的 Flask App 中的下拉框未更新
【发布时间】:2020-05-18 09:43:26
【问题描述】:

我有一个带有客户页面和项目页面的烧瓶网络应用程序。这是工作流程,也是出错的地方。有什么我可能出错的想法吗?

  1. 查看客户页面
  2. 点击“添加客户”> 转到“add_customer”路线
  3. 添加客户信息并点击提交
  4. 客户页面已正确更新为最新的客户名称
  5. 查看项目页面
  6. 点击“添加项目”> 转到“add_project”路线
  7. 此页面有一个客户列表下拉框,但不包括我创建的最后一个客户或自上次启动 Flask 以来我创建的任何客户。

如果我重新启动烧瓶,下拉框会包含最新的客户。起初我以为我没有提交数据库,但似乎我在下面的 db.session.commit 中。我发布了相关代码,如果您想查看其他内容,请告诉我。

我似乎无法在网络上找到我的特定问题,我发现的大部分内容都与填写下拉框本身有关,这确实有效。

forms.py:

class AddProjectForm(FlaskForm):
    projectname = StringField('Project Name', validators=[DataRequired()])
    customer_query = Customer.query.all()
    customers = []
    for c in customer_query:
        customers.append(c.customername)
    customers = SelectField('Customer', choices=customers, default=1)
    submit = SubmitField('Add Project')

routes.py:

@app.route('/add_customer', methods=['GET', 'POST'])
@login_required
def add_customer():
    customer = Customer.query.all()
    form = AddCustomerForm()
    if request.method == 'POST':
        customername = request.values.get('customername') 
        c = Customer(customername=customername)
        db.session.add(c)
        db.session.commit()
        return redirect(url_for('customer'))
    return render_template("add_customer.html", title='Add a Customer', form=form)

@app.route('/add_project', methods=['GET', 'POST'])
@login_required
def add_project():
    form = AddProjectForm()
    if request.method == 'POST':
        projectname = request.values.get('projectname') 
        customer = request.values.get('customers') 
        customer_id = Customer.query.filter_by(customername=customer).first().id
        project = Project(projectname=projectname,customer_id=customer_id)
        db.session.add(project)
        db.session.commit()
        return redirect(url_for('projects'))
    return render_template("add_project.html", title='Add a Project', form=form)

add_project.html

{% extends "base.html" %}

{% block content %}
    <h1>Add a Project</h1>
    <form action="" method="post">
        {{ form.hidden_tag() }}
        <p>
            {{ form.projectname.label }}<br>
            {{ form.projectname }}<br>
        </p>
        <p>
            {{ form.customers.label }}<br>
            {{ form.customers }}<br>
        </p>
        <p>
            {{ form.submit() }}
        </p>
    </form>
{% endblock %}

【问题讨论】:

    标签: python flask sqlalchemy


    【解决方案1】:

    在这里找到答案:

    Flask WTF form not updating with the sqlite3 database

    我从表单中删除了客户列表填充并将其添加到视图中。然后将客户列表传递给模板,并像这样更新模板。

    add_project.html

            <select name="customer" id="customer">
            {% for customer in customers %}
                <option value="{{ customer }}">{{ customer }}</option>
            {% endfor %}
            </select>
    

    现在它正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2011-09-05
      • 2014-09-26
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多