为什么它显示未知命令:'collectstatic',当我尝试收集静态

问题描述

我正在尝试在 Digital Ocean 上部署我的 Django 项目。我在 Digital Ocean 上创建了我的水滴和空间,并创建了一个静态文件夹来存储我的静态文件。我从我的 github-repo提取了我的代码。然后我安装了所有 requirements 并尝试使用命令收集静态文件

python3 manage.py collectstatic

但它显示

UnkNown command: 'collectstatic'
Type 'manage.py help' for usage.

我应该在这里做什么?

我检查了我的 manage.py 助手,但它没有 collectstatic 命令

    check,compilemessages,createcachetable,dbshell,diffsettings,dumpdata,flush,inspectdb,loaddata,makemessages,makemigrations,migrate,runserver,sendtestemail,shell,showmigrations,sqlflush,sqlmigrate,sqlsequencereset,squashmigrations,startapp,startproject,test,testserver,

这些是 manage.py helper 中的命令。

我的 settings.py 如下

import os
from pathlib import Path
from decouple import config

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


DEBUG = config('DEBUG',default=False,cast=bool)

SECRET_KEY = config("SECRET_KEY")

ALLOWED_HOSTS = ["134.209.153.105",]

ROOT_URLconf = f'{config("PROJECT_NAME")}.urls'

# Application deFinition

INSTALLED_APPS = [
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.sites','crispy_forms','accounts','adminn','student','union','chat','channels','allauth','allauth.account','allauth.socialaccount','allauth.socialaccount.providers.google',]
    

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_ENDPOINT_URL = config('AWS_S3_ENDPOINT_URL')
AWS_S3_OBJECT_ParaMETERS = {
    'CacheControl': 'max-age=86400',}
AWS_LOCATION = config('AWS_LOCATION')

STATICFILES_Dirs = [
    os.path.join(BASE_DIR,'static'),]

STATIC_ROOT = os.path.join(BASE_DIR,'static/')
STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL,AWS_LOCATION)
TEMP = os.path.join(BASE_DIR,'temp')
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
  
BASE_URL = "http://134.209.153.105"

在这里,在静态 url AWS_STORAGE_BUCKET_NAME、AWS_S3_ENDPOINT_UR、AWS_LOCATION 中遵循...

AWS_STORAGE_BUCKET_NAME=studentcricle
AWS_S3_ENDPOINT_URL=https://sfo3.digitaloceanspaces.com
AWS_LOCATION=studentcircle-static

解决方法

感谢那些检查我问题的人。 当我运行以下代码时,我的问题解决了。

export DJANGO_SETTINGS_MODULE=mysite.settings

我是从 Django documentation 找到的。 但我仍然没有发现真正的问题是什么。 这与我的设置文件有关。或多个设置文件

所以,如果有人知道详细信息,请在此处描述。或个人。

,

我认为您应该在 settings.py 文件中取消注释 STATIC_ROOT 并尝试以下操作:

STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
,

首先请检查基目录下是否有名为“static”的文件夹;如果是,那么对下面的代码稍作改动,去掉static后面的'/':

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

其次,请确保您已在已安装的应用程序菜单中注册了所有应用程序。

如果问题仍然存在,请尝试运行以下命令:python manage.py shell

如果问题出在设置中,它应该让您知道问题出在哪里。

,

尝试将 settings.py 文件中的 STATIC_ROOT 替换为以下内容:

STATIC_ROOT = '/static/'

并且每当您运行 python3 manage.py collectstatic 命令时,请确保您位于 manage.py 文件所在的基本目录中,并且有一个名为 static 的文件夹。