在 Django 中找不到静态文件

Mil*_*tlt 2 django python-3.x

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

我里面有文件夹static和文件夹landingstatic

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

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

STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    '/Users/milkiweed/Documents/python/django/psy_site/static'
Run Code Online (Sandbox Code Playgroud)

更新。当我尝试使用时,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.
Run Code Online (Sandbox Code Playgroud)

dir*_*ten 5

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

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
Run Code Online (Sandbox Code Playgroud)

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