Django 网站部署引发 ImproperlyConfigured 错误

Django website deployment is raising an ImproperlyConfigured error

我正在使用 Cpanel 配置一个新的 Django 网站。在完成整个程序和 运行 这段代码 python manage.py collectstatic 之后,我收到以下错误:

    Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 195, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 40, in load_command_class
    return module.Command()
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 32, in __init__
    self.storage.path('')
  File "/home/massvnrc/virtualenv/mysite/3.7/lib/python3.7/site-packages/django/contrib/staticfiles/storage.py", line 48, 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.

下面是引发错误的配置:

STATIC_URL = '/static/'
MEDIA_URL='/media/'
STATICFILES_DIRS=[BASE_DIR+"/assets",]
MEDIA_ROOT='/home/****/public_html/media'
STATIC_ROOT='/home/****/public_html/static'

老实说,我不知道错误在哪里,类似的问题也没有帮助我解决问题。

请帮助我

在设置中,静态的正确配置是这样的

STATIC_URL = '/static/'
STATIC_ROOT = '' #This tell where the static file location inside static dirs. Empty means current
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'), #Note this is tuple, and path is /home/../DjangoFolder/static
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

你的文件结构是这样的

DjangoProject #(This is the BASE_DIR path)
 |-manage.py
 |-Django app folder
 |-static #<- you created this folder
 |-media #<- you created this folder

尽可能避免将路径用作字符串,因为在迁移到生产环境时可能会出现问题。相反,使用 'os' library 来获取当前目录路径。通常,如果您按照 Django 中的文档进行配置,所有文件夹都可以从 BASE_DIR 访问。

正如我在您的代码中看到的那样,您需要在 settings.py 中添加 STATIC_ROOT=' ' python manage.py collectstatic 所做的是从目录中收集静态文件。

将您的静态文件夹放入您的应用程序文件夹。

我遇到了同样的情况error.Therefore,我建议你先给你的静态文件路径,然后再运行 collectstatic 命令。

程序如下:

第一步:首先将静态文件放入项目应用程序文件夹。

步骤 2: 添加这个 STATIC_ROOT='path where you want to collect those static files'

您想将那些静态文件提取到哪里。

如果问题仍然存在或您需要更多帮助,请随时发表评论