是否可以在flask中的函数内使用URL变量?我进行了广泛的搜索,并没有更明智。
如果我在html模板中显示if($('#image_comment').is(':empty')){
alert("fhfhf");
$('.well').css('background-color', '#ffffff');
}
,则会显示我要输入的内容。
pathVariable
例如,如果我输入:
127.0.0.1:5000/tryone和127.0.0.1:5000/trytwo,from flask import Flask, request
app = Flask(__name__)
@app.route('/<pathVariable>/')
def test(pathVariable=pathVariable):
test = pathVariable
path = request.path
script_root = request.script_root
base_url = request.base_url
url = request.url
url_root = request.url_root
url_rule = request.url_rule
print ("test is: %s" %path)
print ("path is: %s" %path)
print ("script_root is: %s" %script_root)
print ("base_url is: %s" %base_url)
print ("url is: %s" %url)
print ("url_root is: %s" %url_root)
print ("url_rule is: %s" %url_rule)
return render_template('/example.html', pathVariable=pathVariable)
if __name__ == '__main__':
app.run()
将在html模板中呈现为pathVariable
或tryone
。
但在trytwo
函数中,我得到的打印输出是:
test
我有什么方法可以在test is: /favicon.ico/
path is: /favicon.ico/
script_root is:
base_url is: http://http://127.0.0.1:5000/favicon.ico/
url is: http://http://127.0.0.1:5000/favicon.ico/
url_root is: http://http://127.0.0.1:5000/
url_rule is: /<pathVariable >/
函数中获得tryone
或trytwo
?
我有一个字典,其中包含与每个变量相关联的列表,它们确定必须将哪些变量返回(呈现)回到html模板。
答案 0 :(得分:1)
您正在函数中定义pathVariable
,这似乎是个问题。
将代码更改为:
from flask import Flask, request
app = Flask(__name__)
@app.route('/<pathVariable>/')
def test(pathVariable):
test = pathVariable
path = request.path
script_root = request.script_root
base_url = request.base_url
url = request.url
url_root = request.url_root
url_rule = request.url_rule
print ("test is: %s" %path)
print ("path is: %s" %path)
print ("script_root is: %s" %script_root)
print ("base_url is: %s" %base_url)
print ("url is: %s" %url)
print ("url_root is: %s" %url_root)
print ("url_rule is: %s" %url_rule)
return render_template('/example.html', pathVariable=pathVariable)
if __name__ == '__main__':
app.run()
打印出来
127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /trytwo HTTP/1.1" 301 -
test is: /trytwo/
path is: /trytwo/
script_root is:
base_url is: http://localhost:5000/trytwo/
url is: http://localhost:5000/trytwo/
url_root is: http://localhost:5000/
url_rule is: /<pathVariable>/
127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /trytwo/ HTTP/1.1" 500 -
test is: /favicon.ico/
path is: /favicon.ico/
script_root is:
base_url is: http://localhost:5000/favicon.ico/
url is: http://localhost:5000/favicon.ico/
url_root is: http://localhost:5000/
url_rule is: /<pathVariable>/
127.0.0.1 - - [28/Jan/2016 13:49:02] "GET /favicon.ico/ HTTP/1.1" 500 -
此外,所有网页都会尝试为网站找到网站图标,以便显示的代码不常见。