jQuery .load与python烧瓶 [英] jquery .load with python flask
问题描述
我试图在烧瓶应用中使用jquery .load在每次用户按下按钮添加新的div时添加一个div,但是找不到html文件.
I am trying to use jquery .load in my flask app to add a div every time the user presses a button to add a new div, but it can't find the html file.
有问题的功能.
$(document).ready(function(){
$("#add").click(function(){
$("#form").load('/templates/info.html');
})
});
info.html文件仅包含html的一小段div.
The info.html file only hold a short div section of html.
index.html代码,我要在表单后加载info.html div.
index.html the code, where I want to load the info.html div after the form.
<form id="form" action="{{ url_for('get_data') }}" method="post">
<input type="button" name="add" value="Add" id="add">
<input type="submit" id="submit" value="To XML">
</form>
/templates/info.html
/templates/info.html
<div id="info">
<label>name</label>
<input type="text" name="name">
<br>
<label>asfd</label>
<input type="text" name="asfd">
<br>
</div>
我不断收到此404错误.
I keep getting this 404 error.
127.0.0.1 - - [04/Mar/2014 19:55:50] "GET /templates/info.html HTTP/1.1" 404 -
我正在本地运行flask应用程序,并且/template/info.html是正确的路径.我只是不知道为什么它不能在该html中加载.当我使用jquery函数和.append()并将长长的html字符串放入append函数时,我能够使其工作,但是添加info.html会变得更长,我只是想能够加载整个文件.
I am running the flask app locally, and /template/info.html is the correct path. I just don't know why it doesn't load in that html. I am able to make it work when I use the jquery function and .append() and put the long html string into the append function, but add the info.html gets longer I just want to be able to load the whole file.
任何帮助都将非常感谢.
Any help would be great thanks.
推荐答案
flask应用中的视图定义应返回info.html.换句话说,您的flask应用程序中应具有以下路线和方法:
Your view definition within the flask app should return the info.html. In other words, you should have a route and a method in your flask application as:
@app.route('/info')
def info(name):
return render_template('info.html')
现在,在您的jquery代码中,您可以加载URL /info
.默认情况下,Flask在模板文件夹中查找模板,因此无需在URL中明确提及
Now in your jquery code you can load the url /info
. Flask by default looks for templates in the templates folder, so no need to explicitly mention that in the URL
这篇关于jQuery .load与python烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!