烧瓶应用程序 - 如何将JavaScript文件链接到网站 [英] Flask Application - How to link a javascript file to website

查看:152
本文介绍了烧瓶应用程序 - 如何将JavaScript文件链接到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用我的网站上的JavaScript文件(一个Flask应用程序)开始有一些麻烦。我开始运行run.py,看起来像这样:

 #!flask / bin / python 
from应用程序导入应用程序
app.run(debug = True)

下面的代码给出了一个404错误:



错误消息:



我的文件结构如下所示:





任何提示去错了吗?

解决方案

是的,幸运的是我目前正在开发一个烧瓶应用程序。 b

您目前缺少静态文件夹,默认情况下,文件夹结构如下所示:

  | FlaskApp 
---- | FlaskApp
- ------- |模板
- html文件在这里
-------- | static
- css和javascript文件在这里

Flask将查看模板和静态两个重要的默认文件夹。
一旦你得到了这个排序,你可以使用它来从你的html页面链接到你的javascript文件:

 < script src ={{url_for('static',filename ='somejavascriptfile.js')}}>< / script> 

希望能帮到任何问题。

<加上 - 一个很好的文章阅读,但不是超级相关,但它谈到了瓶的文件夹结构是这样的: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps


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/bin/python
from app import app
app.run(debug=True)

In my html page I have the following code which gives a 404 error:

Error message:

My file structure looks like this:

Any hints to where I'm going wrong?

解决方案

Ah yes, luckily I am currently developing a flask application at the moment.

You are currently missing the static folder 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:

<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

这篇关于烧瓶应用程序 - 如何将JavaScript文件链接到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆