这是我的烧瓶python代码:
@app.route('/ques/<string:idd>',methods=['GET', 'POST'])
def ques(idd):
print idd
Javascript代码:
var counts = {{ test|tojson }};
var text = "";
var texts= "";
for (i = 1; i < counts.length; i=i+2){
texts=counts[i-1];
document.write("<a href=\"{{url_for('ques',idd=texts)}}\">"+counts[i]+"</a></br></br>");
}
如何将idd传递给python函数?
答案 0 :(得分:0)
Jinja在服务器上呈现,因此您无法将其与javascript混合搭配。尝试这样的事情:
var counts = {{ test|tojson }},
// before the HTML file gets to the browser this line will be:
// baseURL = "/ques/REPLACE"
baseURL = "{{ url_for('ques', idd='REPLACE') }}";
for (i = 1; i < counts.length; i = i+2) {
var url = baseURL.replace('REPLACE', counts[i-1]);
document.write("<a href=\"" + url + "\">" +
counts[i] + "</a></br></br>");
}
否则,您需要直接在Javascript中构建URL。
更好的方法是使用请求参数:
@app.route('/ques/')
def ques():
# url is: /ques/?idd=ABC
idd = request.args.get('idd', default='', type=str)
print idd
<强>使用Javascript:强>
var counts = {{ test|tojson }},
baseURL = "{{ url_for('ques') }}";
for (i = 1; i < counts.length; i = i+2) {
var url = baseURL + '?idd=' + counts[i-1];
document.write("<a href=\"" + url + "\">" +
counts[i] + "</a></br></br>");
}