无法在Django中找到静态文件

时间:2017-10-22 14:54:49

标签: django python-3.x

我有macOS Sierra,我修改了我的旧Django项目(项目是在Linux系统上创建的,也许这会引发一些错误)

我在static

中有文件夹landing和文件夹static

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

这是我的settings文件,带有变量static -

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时,我有下一个问题:

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.

1 个答案:

答案 0 :(得分:0)

在您的开发(DEBUG=True)环境中,settings.py需要两件事:

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

这应该足够了,假设您的static目录位于顶级BASE_DIR。如果您的static目录位于其他位置,请相应地调整STATICFILES_DIRS