Flask 使用标记的数据库查询 return 作为参数?
Flask use marked up db query return as argument?
我希望这是可能的。
有没有办法将数据库查询返回的元素作为参数传递给 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'))
在 url_for
docs 返回一个峰值:
It accepts the name of the function as first argument and a number of keyword arguments, each corresponding to the variable part of the URL rule.
url_for
可用于您的 Python 来源:
from flask import url_for
url_for('index')
并在您的模板中:
<a href="{{ url_for('index') }}"></a>
如果您希望 next
方法接收一个值作为 URL 的一部分,那么您可以将方法定义更改为
@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)
我希望这是可能的。
有没有办法将数据库查询返回的元素作为参数传递给 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'))
在 url_for
docs 返回一个峰值:
It accepts the name of the function as first argument and a number of keyword arguments, each corresponding to the variable part of the URL rule.
url_for
可用于您的 Python 来源:
from flask import url_for
url_for('index')
并在您的模板中:
<a href="{{ url_for('index') }}"></a>
如果您希望 next
方法接收一个值作为 URL 的一部分,那么您可以将方法定义更改为
@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)