简体   繁体   English

如何在烧瓶中通过URL路由获取下拉值并更改页面?

[英]How to get a drop down value and change the page by url routing in flask?

I would like to select a value from a drop down and display the web page accordingly. 我想从下拉列表中选择一个值,并相应地显示网页。 Currently I am able to access each value of the drop down by typing it out at the end of the URL. 目前,我可以通过在URL的末尾键入下拉列表来访问每个值。 What exactly am I missing? 我到底在想什么? I am able to access /metadata/table_name1 metadata/table_name2 when I type it out in the browser. 在浏览器中输入/metadata/table_name1 metadata/table_name2时,我可以访问它。 But I am not able to get it when I select the option from the dropdown. 但是,当我从下拉菜单中选择选项时,我将无法获得它。 The dropdown should redirect to the metadata/drop_down_value . 下拉列表应重定向到metadata/drop_down_value

I have tested by printing out individual url routing links. 我已经通过打印出单独的网址路由链接进行了测试。 By clicking the links, it works. 通过单击链接,它可以工作。 I need to select from dropdown. 我需要从下拉列表中进行选择。

Views: 浏览次数:

@app.route('/metadata', methods=['GET', 'POST'])
def metadata():
    cols = None
    table = None
    db_uri = session.get('db_uri', None)
    eng = create_engine(db_uri)
    insp = reflection.Inspector.from_engine(eng)
    tablenames = insp.get_table_names()
    form = SelectTableForm()
    form.table_name.choices = tablenames
    cols = insp.get_columns(table_name=tablenames[0])
    eng.dispose()
    return render_template('tables.html', cols=cols, table=tablenames[0], form=form)

@app.route('/metadata/<table>', methods=['GET', 'POST'])
def select_table(table):
    form = SelectTableForm()
    db_uri = session.get('db_uri', None)
    eng = create_engine(db_uri)
    insp = reflection.Inspector.from_engine(eng)
    tablenames = insp.get_table_names()
    form.table_name.choices = tablenames
    cols = insp.get_columns(table_name=table)
    return render_template('tables.html', cols=cols, table=table, form=form)

Form: 形成:

class SelectTableForm(FlaskForm):

    table_name = SelectField(label='Table name', choices=[], coerce=int)

Jinja html: Jinja html:

<!-- This works -->
{% for table in form.table_name.choices %}
    <a href="{{ url_for('select_table', table=table) }}">{{ table }}</a>
{% endfor %}

<!-- This does not -->

<form action="">
    <select name="tables" method="POST" type="submit">
      {% for table in form.table_name.choices %}
          <option value="{{ url_for('select_table', table=table) }}">{{ table }}</option>
      {% endfor %}
    </select>
</form>

<table>
    <tr>
        <th>table</th>
        <th>name</th>
        <th>type</th>
        <th>nullable</th>
        <th>default</th>
        <th>autoincrement</th>
        <th>comment</th>
    </tr>
    {% for col in cols %}
        <tr>
        <td>{{ table }}</td>
        {% for val in col.values() %}
            <td>{{ val }}</td>
        {% endfor %}
        </tr>
    {% endfor %}
</table>

You need a bit of js where you set the url to the current page to the value of the selected option in the select element: onchange="location = this.value;" 您需要一些js,在其中将当前页面的url设置为select元素中所选选项的值: onchange="location = this.value;" .

from flask import Flask, render_template_string

app = Flask(__name__)

@app.route('/')
def homepage():
    return render_template_string('''
        <select name="form" onchange="location = this.value;">
          {% for table in tables %}
              <option value="{{ url_for('select_table', table=table) }}">{{ table }}</option>
          {% endfor %}
        </select>
''', tables = ['a', 'b'])


@app.route('/select_table/<table>', methods=['GET', 'POST'])
def select_table(table):
    return table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM