Flask 应用程序 - 如何将 javascript 文件链接到网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30011170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask Application - How to link a javascript file to website
提问by user2260199
I am having some trouble getting started with using javascript files on my website (A Flask application). I start the website by running run.py which looks like this:
我在开始使用我的网站(一个 Flask 应用程序)上的 javascript 文件时遇到了一些问题。我通过运行 run.py 来启动网站,如下所示:
#!flask/bin/python
from app import app
app.run(debug=True)
In my html page I have the following code which gives a 404 error:
在我的 html 页面中,我有以下代码,它给出了 404 错误:


Error message:
错误信息:


My file structure looks like this:
我的文件结构如下所示:


Any hints to where I'm going wrong?
任何提示我哪里出错了?
回答by jonprasetyo
Ah yes, luckily I am currently developing a flask application at the moment.
啊,是的,幸运的是我目前正在开发一个烧瓶应用程序。
You are currently missing the staticfolder which by default flask looks into, a folder structure something like this:
您当前缺少默认情况下烧瓶查看的静态文件夹,文件夹结构如下所示:
|FlaskApp
----|FlaskApp
--------|templates
- html files are here
--------|static
- css and javascript files are here
Two important default folders that Flask will look into templates and static. Once you got that sorted you use this to link up with your javascript files from your html page:
Flask 将查看 templates 和static 的两个重要默认文件夹。完成排序后,您可以使用它来链接 html 页面中的 javascript 文件:
<script src="{{url_for('static', filename='somejavascriptfile.js')}}"></script>
Hope that helps any questions just ask.
希望能帮助任何问题只是问。
Plus - A good article to read but not super related but it talks about the folder structure of flask is this: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
另外 - 一篇很好的文章,但不是超级相关,但它谈到了烧瓶的文件夹结构是这样的:https: //www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on -an-ubuntu-vps
回答by dekkard
Try using src="app/javascript/jquery.js".
尝试使用src="app/javascript/jquery.js".
Also, I noticed you include jquery.js twice - on line 7 and 28.
另外,我注意到您两次包含 jquery.js - 在第 7 行和第 28 行。
回答by Anakin
So your index.htmlis at app/templates/index.html, and your jQuery is at app/javascript/jQuery.jsright?
所以你index.html在app/templates/index.html,你的 jQuery 是app/javascript/jQuery.js对的?
I think your path is wrong. Try this:
我认为你的路径是错误的。尝试这个:
<script src="../javascript/jquery.js"></script>
回答by carlos rivera
Here is what helped me eliminate the jquery part of my Flask project
这是帮助我消除 Flask 项目的 jquery 部分的原因
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
This was taken straight from Basic Template found in http://getbootstrap.com/getting-started/

