开发者

how to seperate the script from html template when using jQuery in flask?

I was right at the beginning to learn Flask and following the tutorial at http://flask.pocoo.org/docs/patterns/jquery/ . As the author said, it is usually a better idea to have the script in a separate script file.

But I didn't manage to find out how to do this. I just wrote:

<script type=text/javascript src='foo.js'></script>
<h1>jQuery Example</h1>
<p><input type=text size=5 name=a> +
<input type=text size=5 name=b> =
<span id=result>?</span>
<p><a href=# id=calculate>calculate server side</a>

and in the foo.js I simply copied the content of the first script of the original html code. However when I run the server I got nothing but a blank page, with the log file is "GET /foo.js HTTP/1.1" 304

I'm really a newbie at web programming a开发者_如何转开发nd couldn't find out what was wrong. Thank you guys !


As shown in the loading jQuery section, you need to use the url_for() helper to get the proper location to your script.

<script type="text/javascript" src="{{ url_for('static', filename = 'foo.js') }}"></script>

Something along the lines of the above should work. I have a feeling you may have other issues with the script but the server shouldn't have any more problems finding it.

If it does, please post how you have your directory structure setup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜