Django 找不到我的静态文件

iFe*_*ngo 6 python django static web twitter-bootstrap

我对网络开发还比较陌生。我正在尝试构建我的第一个网络应用程序。我的静态文件夹位于 project_root/static 中,但由于某种原因,当我运行服务器时,我不断收到 404 错误:

未找到:例如 /users/login/js/bootstrap.min.js。

我的 html 顶部有 {% load staticfiles %} ,在我的 settings.py 中我有:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
STATIC_URL = '/static/'                                                                                        
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Run Code Online (Sandbox Code Playgroud)

谢谢。


编辑:通过以下答案的组合修复。谢谢你们!

zap*_*.10 6

static将静态 js、css 和其他文件放入项目目录中名为的目录中,然后这些设置将起作用:

# this defines the url for static files
# eg: base-url.com/static/your-js-file.js
STATIC_URL = '/static/'

# this is directory name where collectstatic files command will put your app level static files
STATIC_ROOT = 'staticfiles'

# this is directory paths where you have to put your project level static files
# you can put multiple folders here
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
Run Code Online (Sandbox Code Playgroud)

根据这些设置,您将能够正确访问静态文件


Moh*_*agi 2

继续阅读此内容 - http://agiliq.com/blog/2013/03/serving-static-files-in-django/

摘要:如果“users”是项目中的应用程序名称,则创建目录 - 'users/static/js/'。并将 bootstrap.min.js 放入 js/ 中。该文件可在 localhost:8000/static/js/bootstrap.min.js 访问。来吧,尝试一下。