Django网站部署引发了错误配置的错误

问题描述 投票:0回答:2

我正在使用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'

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

请帮助我

django python-3.x cpanel
2个回答
0
投票

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

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”库获取当前目录路径。通常,如果您按照Django中的说明进行配置,则可以从BASE_DIR访问所有文件夹。


0
投票

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

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

我遇到了相同的错误。因此,建议您先给静态文件路径,然后再运行collectstatic命令。

这里是步骤:

[Step1:首先将静态文件放入项目应用程序文件夹。

Step2:添加此STATIC_ROOT ='您要在其中收集这些静态文件的路径'

您想在哪里提取这些静态文件。

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

© www.soinside.com 2019 - 2024. All rights reserved.