Python 如何通过flask中的url路由获取下拉值并更改页面?
我想从下拉列表中选择一个值,并相应地显示网页。目前,我可以通过在URL末尾键入下拉列表的每个值来访问它。我到底错过了什么?当我在浏览器中键入元数据时,我可以访问Python 如何通过flask中的url路由获取下拉值并更改页面?,python,html,flask,jinja2,Python,Html,Flask,Jinja2,我想从下拉列表中选择一个值,并相应地显示网页。目前,我可以通过在URL末尾键入下拉列表的每个值来访问它。我到底错过了什么?当我在浏览器中键入元数据时,我可以访问/metadata/table_name1metadata/table_name2。但是当我从下拉列表中选择该选项时,我无法获得它。下拉列表应重定向到元数据/下拉列表值 我已经通过打印出单独的url路由链接进行了测试。通过单击链接,它可以工作。我需要从下拉列表中选择 观点: @app.route('/metadata', methods=
/metadata/table_name1
metadata/table_name2
。但是当我从下拉列表中选择该选项时,我无法获得它。下拉列表应重定向到元数据/下拉列表值
我已经通过打印出单独的url路由链接进行了测试。通过单击链接,它可以工作。我需要从下拉列表中选择
观点:
@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)
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>
{form.table_name.choices%}
{%endfor%}
{form.table_name.choices%}
{{table}}
{%endfor%}
桌子
名称
类型
可空
违约
自动增量
评论
{cols%中的col的%s}
{{table}}
{col.values()中val的%s}
{{val}}
{%endfor%}
{%endfor%}
您需要一点js,将当前页面的url设置为select元素中所选选项的值:onchange=“location=this.value;”“
从烧瓶导入烧瓶,呈现\u模板\u字符串
app=烧瓶(名称)
@应用程序路径(“/”)
def homepage():
返回渲染模板字符串(“”)
{对于表%%中的表,%}
{{table}}
{%endfor%}
'',表格=['a','b'])
@app.route('/select_table/',methods=['GET','POST'])
def select_表格(表格):
返回表
嘿,非常感谢。这很有效。你能帮我再做一件小事吗?假设我从下拉列表中选择val_3
。如何让下拉列表在val_3
页面上显示val_3
而不是val_1
?在jinja for循环中,您执行
,其中当前呈现的_表
是您当前显示的表的ID。ID是
标记中的ID
属性?如果是的话,那么它就不是那样工作的。当我按下后退按钮时,val_3
显示在上一页,而不是当前页。当前页面仍然只显示第一个值。不,id是您自己用来标识表的内容。它与html属性无关。因此,您只需检查当前循环的表是否与您可能呈现的表相同。是的,提供一个名为table
的变量,并执行{%for table in tables%}
,这就是我使用这种方式来编写它的原因(我自己懒得重命名它们)。关于学习js,我自己也不知道,我只是在谷歌上搜索了一下,然后绊倒了厄普顿:)
<!-- 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>
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