和老师说的一样,由于新版本的cookiecutter生成项目时,production.py 中多了这几行
# Collectfast
# ------------------------------------------------------------------------------
# https://github.com/antonagestam/collectfast#installation
INSTALLED_APPS = ["collectfast"] + INSTALLED_APPS # noqa F405
这段代码自动将collectfast注册到 base.py 的INSTALLED_APPS中,并且符合collectfast的要求:
确保在您的设置文件中包含此文件,
并在'django.contrib.staticfiles'之前将'collectfast'添加到 INSTALLED_APPS中:
这会要求你使用collectfast来更快的执行collectstatic,来看collectfast描述
A faster collectstatic command.
随着越来越多的文件添加到项目中,运行Django的collectstatic命令可能会变得非常缓慢,
特别是当源代码中包含jQuery UI之类的繁重库时。Collectfast自定义内置的 collectstatic命令,
添加了不同的优化功能,以使上传大量文件的速度更快。
所以执行collectstatic会报错,提示没有安装collectfast,这是因为INSTALLED_APPS中添加了collectfast
,但是我们并没有安装,当使用 pipenv install Collectfast,完成安装后,再次执行collectstatic,会出现如图所示的错误,根据Collectfast文档,在base.py 末尾添加2行代码如下:
STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy"
# 需要botocore,boto3模块
pipenv install botocore
pipenv install boto3
再次执行collectstatic依旧报错,┑( ̄Д  ̄)┍,报错如下
# 输入指令
[root@yun zanhu]# pipenv run python3 manage.py collectstatic
Loading .env environment variables…
/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/storages/backends/s3boto3.py:283: UserWarning: The default behavior of S3Boto3Storage is insecure and will change in django-storages 2.0. By default files and new buckets are saved with an ACL of 'public-read' (globally publicly readable). Version 2.0 will default to using the bucket's ACL. To opt into the new behavior set AWS_DEFAULT_ACL = None, otherwise to silence this warning explicitly set AWS_DEFAULT_ACL.
"The default behavior of S3Boto3Storage is insecure and will change "
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?
# 输入yes后
Type 'yes' to continue, or 'no' to cancel: yes
# 报错详细信息
Traceback (most recent call last):
File "manage.py", line 30, in <module>
execute_from_command_line(sys.argv)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/management/commands/collectstatic.py", line 90, in handle
ret = super().handle(**options)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
collected = self.collect()
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/management/commands/collectstatic.py", line 80, in collect
ret = super().collect()
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
handler(path, prefixed_path, storage)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/management/commands/collectstatic.py", line 122, in copy_file
self.maybe_copy_file(args)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/management/commands/collectstatic.py", line 105, in maybe_copy_file
if not self.strategy.should_copy_file(path, prefixed_path, source_storage):
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/strategies/base.py", line 111, in should_copy_file
remote_hash = self.get_cached_remote_file_hash(path, prefixed_path)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/strategies/base.py", line 125, in get_cached_remote_file_hash
hash_ = self.get_remote_file_hash(prefixed_path)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/collectfast/strategies/boto3.py", line 40, in get_remote_file_hash
hash_ = self.remote_storage.bucket.Object(
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/django/utils/functional.py", line 225, in inner
return func(self._wrapped, *args)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/storages/backends/s3boto3.py", line 327, in bucket
self._bucket = self._get_or_create_bucket(self.bucket_name)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/storages/backends/s3boto3.py", line 364, in _get_or_create_bucket
bucket = self.connection.Bucket(name)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/boto3/resources/factory.py", line 474, in create_resource
client=self.meta.client)(*args, **kwargs)
File "/root/.local/share/virtualenvs/zanhu-qCBWC76o/lib/python3.7/site-packages/boto3/resources/base.py", line 119, in __init__
'Required parameter {0} not set'.format(identifier))
ValueError: Required parameter name not set
。。。这个问题求助老师。。。
非正常解决办法:绕过collectfast,将production.py中的代码注释掉,不使用collectfast,执行collectstatic则一切正常