首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Django中找不到静态文件

在Django中找不到静态文件
EN

Stack Overflow用户
提问于 2017-10-22 14:54:48
回答 1查看 2.4K关注 0票数 2

我有macOS塞拉利昂,我修改了以前的Django项目(项目是在Linux系统上创建的,也许这会调用一些bug)。

我有文件夹static和文件夹landingstatic

当我运行服务器时,它会打印出来:GET /static/landing/img/397x300/03.jpg HTTP/1.1" 404 1691

这是我的带有变量静态的settings文件-

代码语言:javascript
运行
复制
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    '/Users/milkiweed/Documents/python/django/psy_site/static'

UPD。当我尝试使用python3 manage.py collectstatic时,我有下一个问题:

代码语言:javascript
运行
复制
You have requested to collect static files at the destination
location as specified in your settings.

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/core/management/__init__.py", line 364, in     execute_from_command_line
    utility.execute()
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/contrib/staticfiles/management/commands/collectstatic.py"    , line 199,in handle
    collected = self.collect()
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/contrib/staticfiles/management/commands/collectstatic.py"    , line 124,in collect
    handler(path, prefixed_path, storage)
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/contrib/staticfiles/management/commands/collectstatic.py"    , line 354,in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/contrib/staticfiles/management/commands/collectstatic.py"    , line 260,in delete_file
    if self.storage.exists(prefixed_path):
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/core/files/storage.py", line 392, in exists
    return os.path.exists(self.path(name))
  File "/Users/milkiweed/Documents/vn/psychology/lib/python3.6/site-    packages/django/contrib/staticfiles/storage.py", line 50, in path
    raise ImproperlyConfigured("You're using the staticfiles app "
django.core.exceptions.ImproperlyConfigured: You're using the     staticfiles app without having set the STATIC_ROOT setting to a     filesystem path.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-22 16:00:21

在您的开发(DEBUG=True)环境中,在settings.py中需要两样东西

代码语言:javascript
运行
复制
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

假设您的static目录位于顶层的BASE_DIR中,这就足够了。如果您的static目录位于其他地方,则相应地调整STATICFILES_DIRS

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46875481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档