我希望这是可能的。
有没有办法将返回的元素从db查询作为参数传递给url_for?
它正在被jinja模板标记,但它不是表单元素。
@app.route('/matcher/', methods=['GET', 'POST'])
def show_entries():
#global account_id
global load_file_id
if request.method == 'GET':
option = str(request.args.get('button'))
load_file_id = int(request.args.get('a'))
if option == 'All':
sql = """SELECT *
FROM TABLE WHERE LOAD_FILE_ID = :load_file_id"""
c = g.db.cursor()
c.execute(sql, load_file_id=load_file_id)
rows = c.fetchall()
for row in rows:
entries = [dict(title=row[0], text=row[1], text1=row[2], text2=row[3], text3=row[4], text4=row[5], text5=row[6], text6=row[7])]
media = row[8]
account_id = int(row[0])
c.execute("""UPDATE TABLE SET STATUS = 'PENDING' WHERE ACCOUNT_ID = :account_id""", account_id=account_id)
g.db.commit()
outs = mediapod(account_id)
return render_template('show_entries.html', entries=entries, media=media, outs=outs, src=src)
c.close()
elif:
############DOESN'T MATTER#############
然后
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
{% for entry in entries %}
<div class=entry>
<form action="{{ url_for('next') }}" method=get
class=validate>
<h2 name=id>{{ entry.title }}</h2>
<div class=subentry>
<div class=titles>
INFO:<br>
INFO:<br>
INFO:<br>
INFO:<br>
INFO:<br>
INFO:<br>
INFO:<br></div>
<div class=elements>
${{ entry.text|string }}<br>
{{ entry.text1|string }}<br>
{{entry.text2|string }}<br>
{{ entry.text3|string }}<br>
{{ entry.text4|string }}<br>
{{ entry.text5|string }}<br>
{{ entry.text6|string }}<br></div>
<div class=mediatable>
<table>
<tr>
<th>Type</th>
<th>Date</th>
<th>Status</th>
<th>Doc ID</th>
<th>Scan Date</th>
</tr>
{% for put in outs %}
<tr>
<td>{{ put.media_type|safe }}</td>
<td>{{ put.statement_date|safe }}</td>
<td>{{ put.media_status|safe }}</td>
<td>{{ put.doc_id|safe }}</td>
<td>{{ put.scan_date|safe }}</td>
</tr>
{% endfor %}
</table>
</div>
<div class=buttons>
<input type=radio name=box value=match>Match
<input type=radio name=box value=nomatch>No Match
<input type=radio name=box value=fup>Follow-Up
<input type=radio name=box value=nomedia>No Media
<input type=submit class=next name=submit value=Next>
</div>
</div>
</form>
</div>
{% else %}
<em>stuff</em>
{% endfor %}
{% endif %}
{% endblock %}
然后我有一条路线需要从show_entries()获取account_id
#Next Button
@app.route('/next', methods=['GET'])
def next():
status = request.args.get('box')
c = g.db.cursor()
c.execute("""UPDATE TABLE
SET STATUS = :status
WHERE ACCOUNT_ID = :account_id""",
{"status" : str(status),
"account_id" : account_id
})
g.db.commit()
return redirect(url_for('nextnext'))
答案 0 :(得分:0)
它接受函数的名称作为第一个参数和一些关键字参数,每个参数对应于URL规则的变量部分。
url_for
可以在您的Python源代码中使用:
from flask import url_for
url_for('index')
并在您的模板中:
<a href="{{ url_for('index') }}"></a>
如果您希望next
方法接收值作为网址的一部分,那么您可以将方法定义更改为
@app.route('/next/<int:account_id>', methods=['GET'])
def next(account_id):
# ...
然后您可以通过url_for
调用从Python源代码或模板中传递变量:
url_for('next', account_id=account_id)
如果您需要show_entries
视图在模板中包含该值,请使用render_template
调用传递值:
return render_template('show_entries.html', entries=entries, media=media, outs=outs, src=src, account_id=account_id)