最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - flask can not read static path and load Javascript files - Stack Overflow

programmeradmin1浏览0评论

my app structure is

project/
       configuration/__init__.py
       core/
       static
             /app
                 /css
                 /img
                 /js/app.js
                 /lib/angular/angular.js
                 /partials
             index.html

My file configuration/__init__.py has something as

# setting up template directory
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../static/app')

app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=TEMPLATE_DIR)

When I load the page localhost/index.html, I see javascript error as

GET http://127.0.0.1:5000/js/app.js 404 (NOT FOUND) home:8
GET http://127.0.0.1:5000/lib/angular/angular.js 404 (NOT FOUND) home:8

Even though I know that these files are there, I guess that static_folder is not able to find these files, what am I doing wrong here

my app structure is

project/
       configuration/__init__.py
       core/
       static
             /app
                 /css
                 /img
                 /js/app.js
                 /lib/angular/angular.js
                 /partials
             index.html

My file configuration/__init__.py has something as

# setting up template directory
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../static/app')

app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=TEMPLATE_DIR)

When I load the page localhost/index.html, I see javascript error as

GET http://127.0.0.1:5000/js/app.js 404 (NOT FOUND) home:8
GET http://127.0.0.1:5000/lib/angular/angular.js 404 (NOT FOUND) home:8

Even though I know that these files are there, I guess that static_folder is not able to find these files, what am I doing wrong here

Share Improve this question asked Apr 21, 2013 at 6:11 daydreamerdaydreamer 92.3k204 gold badges473 silver badges750 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I fixed it by putting relative urls in index.html

configuration/_init_.py

# setting up template directory
ASSETS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../static/app')

app = Flask(__name__, template_folder=ASSETS_DIR, static_folder=ASSETS_DIR)

index.html

  <script src="../app/lib/angular/angular.js"></script>
  <script src="../app/js/app.js"></script>
发布评论

评论列表(0)

  1. 暂无评论