尝试部署django时出现静态文件问题

时间:2018-02-24 06:23:29

标签: python django ubuntu

我正在尝试在this教程之后部署Django但是收到错误。当教程说运行python manage.py collectstatic时,如果没有建议的进一步提示,而不是collectstatic运行,我得到以下内容:

/home/elections/venv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)

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 "/home/elections/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/elections/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 199, in handle
    collected = self.collect()
  File "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 124, in collect
    handler(path, prefixed_path, storage)
  File "/home/elections/venv/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 "/home/elections/venv/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 260, in delete_file
    if self.storage.exists(prefixed_path):
  File "/home/elections/venv/lib/python3.6/site-packages/django/core/files/storage.py", line 392, in exists
    return os.path.exists(self.path(name))
  File "/home/elections/venv/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 :(得分:1)

如前所述,您需要在settings.py中定义STATIC_ROOT变量才能使collectstatic正常工作。

  

collectstatic将收集的目录的绝对路径   用于部署的静态文件。   示例:“/ var / www / example / static /”

您也可以定义STATICFILES_DIRS

您发布的教程中的nginx.conf期望从/home/boards/staticfiles/提供收集的静态文件。由于本教程将所有内容部署在托管数据库(postgres),Web服务器(nginx)和应用程序(django)的单个整体服务器上,因此您可以按如下方式进行设置: / p>

STATIC_ROOT = '/home/boards/staticfiles/'