python-3.x - 我想解决 ubuntu 服务器中的 django webserver collectstatic 错误

标签 python-3.x django ubuntu nginx gunicorn

我尝试让静态文件使用'python manage.py collectstatic',但它不起作用。
我在 Ubuntu 20.04.4 中运行我的 django 项目, 并使用 Nginx作为网络服务器,Guicorn用于 WSGI 服务器。
这是我的错误日志,

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>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/core/files/storage.py", line 362, in exists
    return os.path.lexists(self.path(name))
  File "/home/devadmin/venvs/bio_platform/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 39, in path
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
我从谷歌找到了很多解决方案,我猜这是因为我没有设置 STATIC_ROOT适本地。
这是我的项目目录结构和下面的代码;
└── bio_platform
    ├── common
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-310.pyc
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── __init__.cpython-39.pyc
    │   │   ├── admin.cpython-310.pyc
    │   │   ├── admin.cpython-38.pyc
    │   │   ├── admin.cpython-39.pyc
    │   │   ├── apps.cpython-310.pyc
    │   │   ├── apps.cpython-38.pyc
    │   │   ├── apps.cpython-39.pyc
    │   │   ├── forms.cpython-310.pyc
    │   │   ├── forms.cpython-38.pyc
    │   │   ├── forms.cpython-39.pyc
    │   │   ├── models.cpython-310.pyc
    │   │   ├── models.cpython-38.pyc
    │   │   ├── models.cpython-39.pyc
    │   │   ├── urls.cpython-310.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   ├── urls.cpython-39.pyc
    │   │   ├── views.cpython-310.pyc
    │   │   ├── views.cpython-38.pyc
    │   │   └── views.cpython-39.pyc
    │   ├── admin.py
    │   ├── apps.py
    │   ├── forms.py
    │   ├── migrations
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   ├── models.py
    │   ├── tests.py
    │   ├── urls.py
    │   └── views.py
    ├── config
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-310.pyc
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── __init__.cpython-39.pyc
    │   │   ├── settings.cpython-310.pyc
    │   │   ├── settings.cpython-38.pyc
    │   │   ├── settings.cpython-39.pyc
    │   │   ├── urls.cpython-310.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   ├── urls.cpython-39.pyc
    │   │   ├── wsgi.cpython-310.pyc
    │   │   ├── wsgi.cpython-38.pyc
    │   │   └── wsgi.cpython-39.pyc
    │   ├── asgi.py
    │   ├── settings
    │   │   ├── __pycache__
    │   │   ├── base.py
    │   │   ├── local.py
    │   │   └── prod.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── db.sqlite3
    ├── manage.py
    ├── node_modules
    │   └── bootstrap
    │       ├── LICENSE
    │       ├── README.md
    │       ├── dist
    │       ├── js
    │       ├── package.json
    │       └── scss
    ├── package-lock.json
    ├── pybo
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-310.pyc
    │   │   ├── __init__.cpython-38.pyc
    │   │   ├── __init__.cpython-39.pyc
    │   │   ├── admin.cpython-310.pyc
    │   │   ├── admin.cpython-38.pyc
    │   │   ├── admin.cpython-39.pyc
    │   │   ├── apps.cpython-310.pyc
    │   │   ├── apps.cpython-38.pyc
    │   │   ├── apps.cpython-39.pyc
    │   │   ├── forms.cpython-310.pyc
    │   │   ├── forms.cpython-38.pyc
    │   │   ├── forms.cpython-39.pyc
    │   │   ├── models.cpython-310.pyc
    │   │   ├── models.cpython-38.pyc
    │   │   ├── models.cpython-39.pyc
    │   │   ├── urls.cpython-310.pyc
    │   │   ├── urls.cpython-38.pyc
    │   │   ├── urls.cpython-39.pyc
    │   │   └── views.cpython-310.pyc
    │   ├── admin.py
    │   ├── apps.py
    │   ├── forms.py
    │   ├── migrations
    │   │   ├── 0001_initial.py
    │   │   ├── 0002_question_author.py
    │   │   ├── 0003_answer_author.py
    │   │   ├── 0004_answer_modify_date_question_modify_date.py
    │   │   ├── 0005_comment.py
    │   │   ├── 0006_answer_voter_question_voter_alter_answer_author_and_more.py
    │   │   ├── 0007_auto_20220411_1325.py
    │   │   ├── __init__.py
    │   │   └── __pycache__
    │   ├── models.py
    │   ├── templatetags
    │   │   ├── __pycache__
    │   │   └── pybo_filter.py
    │   ├── tests.py
    │   ├── urls.py
    │   └── views
    │       ├── __pycache__
    │       ├── answer_views.py
    │       ├── base_views.py
    │       ├── comment_views.py
    │       ├── question_views.py
    │       └── vote_views.py
    ├── static
    │   ├── bootstrap.min.css
    │   ├── bootstrap.min.js
    │   ├── jquery-3.6.0.min.js
    │   └── style.css
    ├── templates
    │   ├── base.html
    │   ├── common
    │   │   ├── login.html
    │   │   └── signup.html
    │   ├── form_errors.html
    │   ├── navbar.html
    │   └── pybo
    │       ├── answer_form.html
    │       ├── comment_form.html
    │       ├── question_detail.html
    │       ├── question_form.html
    │       └── question_list.html
    └── winehq.key

如您所见,我像这样划分设置文件,
因为我想经营服务器开发环境本地开发环境分别地。
├── settings
    │   │   ├── __pycache__
    │   │   ├── base.py
    │   │   ├── local.py
    │   │   └── prod.py
这是我设置文件的完整代码
(我删除了类似于 IP 地址的私有(private)部分)。
base.py
"""
Django settings for config project.

Generated by 'django-admin startproject' using Django 4.0.3.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'blank' #private part

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['blank'] #private part


# Application definition

INSTALLED_APPS = [
    'common.apps.CommonConfig',
    'pybo.apps.PyboConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'config.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'ko-kr'

TIME_ZONE = 'Asia/Seoul'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'
#STATIC_ROOT = os.path.join(BASE_DIR,'static')

STATICFILES_DIRS = [    
    BASE_DIR / 'static',
    ]

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
本地.py
from .base import *

ALLOWED_HOSTS = []
prod.py
from .base import *
import os

ALLOWED_HOSTS = ['blank'] #private part
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
#STATIC_ROOT = BASE_DIR / 'static/'
STATICFILES_DIRS = []
# concept addon

另外,这是我的bio_platform.service用于 nignx 服务器。
bio_platform.service
server {
    listen 80;
    server_name blank; #private part

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static {
        alias /home/devadmin/projects/bio_platform/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/tmp/gunicorn.sock;
    }
}

我做了一些在 google 和 stackoverflow 中给出的解决方案,例如:
我尝试了什么

set a STATIC_ROOT and STATIC_URL on prod.py set a STATIC_ROOT and STATIC_URL on base.py


但如您所知,它不起作用,django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.错误仍然存​​在。
要知道,我不确定我在哪里设置 STATIC_ROOTSTATIC_URL , STATICFILES_DIR .
请大家帮帮我......

最佳答案

我确实在我的项目中发现了我的错误。
问题是由从 venvs 文件夹自动带来 prod.py 设置的路径设置引起的。
我在路径下做了一个 bio_platform.sh 文件 ~/venvs/ 之前

#!/bin/bash

cd ~/projects/bio_paltform
export DJANGO_SETTINGS_MODULE=config.settings.prod
. ~/venvs/bio_paltform/bin/activate
正如你们所看到的,我输入错误 bio_paltform .
所以我改变了bio_paltformbio_platform .
之后
#!/bin/bash

cd ~/projects/bio_platform
export DJANGO_SETTINGS_MODULE=config.settings.prod
. ~/venvs/bio_platform/bin/activate
我修改这个文件后,python managy.py collectstatic秩序就是工作。
$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /home/devadmin/projects/bio_platform/static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

128 static files copied to '/home/devadmin/projects/bio_platform/static'.

关于python-3.x - 我想解决 ubuntu 服务器中的 django webserver collectstatic 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71933056/

相关文章:

linux - rsync 命令有效,但不能作为 cronjob

python - python中带大括号的参数

django-taggit 从查询集中创建 tagcloud

arrays - python数组的第一个和第二个元素的紧凑形式

django - 如何正确地使用 ManyToManyField 覆盖模型的 ModelForm 的 save() 方法

django - Facebook 链接分享问题

linux - 我如何在 AWS 的 amazon linux 上使用 Atom 编辑器?

ruby - "in ` 需要 ': cannot load such file -- iconv (LoadError)"

python-3.x - 在包装器中避免 flake8-bugbear 的 B006(即可变默认警告)的预期方法是什么?

python-3.x - Docker 上的 Jupyter 内核网关中的 RuntimeError