我正在使用django框架开发网站。
所以我的静态文件中有3个文件夹,如下所示。
*web
*home
*static
*admin
*scripts
*styles
*web
*manage.py
*db.sqlite3
我的设置文件有静态设置
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
但是当我键入python3 manage.py collectstaitc时,我得到了这个结果
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/pwanhsu/django/django/core/management/__init__.py", line 349, in execute_from_command_line
utility.execute()
File "/Users/pwanhsu/django/django/core/management/__init__.py", line 341, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/pwanhsu/django/django/core/management/base.py", line 290, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/pwanhsu/django/django/core/management/base.py", line 341, in execute
output = self.handle(*args, **options)
File "/Users/pwanhsu/django/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
collected = self.collect()
File "/Users/pwanhsu/django/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
File "/Users/pwanhsu/django/django/contrib/staticfiles/finders.py", line 112, in list
for path in utils.get_files(storage, ignore_patterns):
File "/Users/pwanhsu/django/django/contrib/staticfiles/utils.py", line 28, in get_files
directories, files = storage.listdir(location)
File "/Users/pwanhsu/django/django/core/files/storage.py", line 286, in listdir
for entry in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/pwanhsu/Desktop/v_bridge/web/web/static'
如果我删除像这样的STATICFILES_DIRS
,我可以成功收集静态文件# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
0 static files copied to '/Users/pwanhsu/Desktop/v_bridge/web/static', 56 unmodified.
在我的html文件中,我有这些代码
{%load staticfiles%}
但服务器返回404错误
[08/Sep/2016 03:36:45] "GET / HTTP/1.1" 200 5831
[08/Sep/2016 03:36:45] "GET /static/scripts/bootstrap/css/bootstrap-responsive.min.css HTTP/1.1" 404 1763
[08/Sep/2016 03:36:45] "GET /static/scripts/icons/social/stylesheets/social_foundicons.css HTTP/1.1" 404 1775
[08/Sep/2016 03:36:45] "GET /static/scripts/icons/general/stylesheets/general_foundicons.css HTTP/1.1" 404 1781
任何人都知道如何设置静态路径或我做错了什么。真的很混淆。
答案 0 :(得分:1)
使用 STATICFILES_DIRS =('。/ static',) 或
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static' ), )
答案 1 :(得分:1)
您的settings.py
位于web/web/
目录中。您static
目录位于web/
目录中。在这种情况下设置:
STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'static))
STATICFILES_DIRS定义静态文件的其他位置。您不需要此设置。
答案 2 :(得分:0)
尝试在staticfiles_dir中使用BASE_DIR:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
或
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'web/static')]
答案 3 :(得分:0)
STATIC_ROOT='static'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(SITE_ROOT ,'/static/'),
]
这实际上是我的一个项目的片段。在开始时,我也一直在努力处理静态文件。这应该可以解决你的问题。如果你仍然得到404.检查静态文件目录和静态文件的权限