flask forms and javascript
flask forms and javascript
你好,我有下面的html代码
<form>
<input type="text" id="filepathz" size="40" placeholder="Spot your project files">
<input type="button" id="spotButton" value="Spot">
</form>
javascript代码
window.onload = init;
function init() {
var button = document.getElementById("spotButton");
button.onclick = handleButtonClick;
}
function handleButtonClick(e) {
var filepathz = document.getElementById("filepathz");
var path = filepathz.value;
if (path == "") {
alert("give a filepath");
}else{
var url = "http://localhost:5000/tx/checkme/filepathz=" + path;
window.open (url,'_self',false);
}
}
和烧瓶上的python代码
def index():
"""Load start page where you select your project folder
or load history projects from local db"""
from txclib import get_version
txc_version = get_version()
prj = project.Project(path_to_tx)
# Let's create a resource list from our config file
res_list = []
prev_proj = ''
for idx, res in enumerate(prj.get_resource_list()):
hostname = prj.get_resource_host(res)
username, password = prj.getset_host_credentials(hostname)
return render_template('init.html', txc_version=txc_version, username=username)
@app.route('/tx/checkme/<filepathz>')
def checkme(filepathz):
filepathz = request.args.get('filepathz')
return render_template('init.html', txc_version=filepathz)
我做错了什么,无法从表单(filepathz)中获取数据<---我没有
您没有正确传递变量。传递变量有两种方法:
1) 通过获取方法传递:
http://localhost:5000/tx/checkme/?filepathz=" + path; (Note the '?')
目前,您正在尝试从request.args获取变量,但没有在请求中传递它,这就是为什么您没有得到任何变量的原因。
2) 从带有flask的url结构的url中获取:
在JS:http://localhost:5000/tx/checkme/" + path
中执行此操作
在你看来:
@app.route('/tx/checkme/<filepathz>')
def checkme(filepathz):
return render_template('init.html', txc_version=filepathz) # You can use this variable directly since you got it as a function arguement.
相关文章:
- JavaScript and LiveView (TIBCO Live Datamart)
- Javascript and onMouseOver
- javascript and css firefox issue
- geolocation with javascript and ruby on rails
- Javascript and the DOM issues
- Javascript and Java Communication
- Javascript and Knockout
- Javascript and Css: Height does not change
- javascript and Internet Explorer 8
- Javascript and html, UI construction
- Javascript and Lightbox
- javascript and css styling
- JavaScript and URL parameters
- Javascript and singleton pattern
- Javascript and Ajax Calls
- OAuth, javascript and many URI's
- Javascript and AJAX post method
- javascript and web services WSDL
- javascript and webview in android
- Javascript & and | symbols