簡體   English   中英

在 django 上更改數據庫后,“運行 '$ python manage.py collectstatic --noinput' 時出錯”

[英]"Error while running '$ python manage.py collectstatic --noinput'" after changing database on django

我是 Django 的初學者,所以如果我遺漏了一些必要的信息,請原諒我。 我最近在 settings.py 上修改了我的數據庫,但在運行時不斷出現錯誤

    git push heroku master

錯誤塊看起來像:

    Counting objects: 22, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (22/22), done.
Writing objects: 100% (22/22), 4.84 KiB | 825.00 KiB/s, done.
Total 22 (delta 12), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote:  !     Python has released a security update! Please consider upgrading to python-3.6.8
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing requirements with pip
remote: 
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
remote:            import psycopg2 as Database
remote:        ModuleNotFoundError: No module named 'psycopg2'
remote:        During handling of the above exception, another exception occurred:
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv   )
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line  
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
remote:            app_config.import_models()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
remote:            self.models_module = import_module(models_module_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
remote:            from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
remote:            class AbstractBaseUser(models.Model):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 101, in __new__
remote:            new_class.add_to_class('_meta', Options(meta, app_label))
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py", line 305, in add_to_class
remote:            value.contribute_to_class(cls, name)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/options.py", line 203, in contribute_to_class
remote:            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
remote:            return getattr(connections[DEFAULT_DB_ALIAS], item)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
remote:            backend = load_backend(db['ENGINE'])
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
remote:            return import_module('%s.base' % backend_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:             return _bootstrap._gcd_import(name[level:], package, level)
remote:           File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/  base.py", line 24, in <module>
remote:            raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
remote:        django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module    named 'psycopg2'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to ewbunlv.
remote: 
To https://git.heroku.com/ewbunlv.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/ewbunlv.git'

這是我更改的 settings.py 的一部分(最初是 Django 的默認數據庫)

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

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', #Used 'django.db.backends.postgresql' as well
            'NAME': 'myname',
            USER': 'user',
            'PASSWORD': 'password',
            'HOST': 'host',
            'PORT': '5432',
        }
    }

我的要求.txt:

    awscli==1.16.91
boto==2.49.0
boto3==1.9.81
botocore==1.12.81
certifi==2018.11.29
chardet==3.0.4
colorama==0.3.9
dj-database-url==0.5.0
Django==2.1.4
django-cleanup==3.0.1
django-s3-folder-storage==0.5
django-storages==1.7.1
docutils==0.14
gunicorn==19.9.0
heroku==0.1.4
idna==2.8
jmespath==0.9.3
Pillow==5.3.0
pyasn1==0.4.5
python-dateutil==2.7.5
pytz==2018.7
PyYAML==3.13
requests==2.21.0
rsa==3.4.2
s3transfer==0.1.13
six==1.12.0
urllib3==1.24.1
whitenoise==4.1.2

我跑了

    heroku run python manage.py collectstatic --dry-run --noinput

它工作得很好,沒有任何錯誤。 我已經為此工作了一段時間,但無濟於事。

它在本地也能正常工作,但是當我嘗試將其轉換為生產時,出現上述錯誤。 我錯過了什么?

  1. 在部署期間禁用 collectstatic
heroku config:set DISABLE_COLLECTSTATIC=1
  1. 部署
git push heroku master (or git push heroku <branch>:master if not on master)
  1. 運行遷移
heroku run python manage.py migrate
  1. 使用 bower 運行 collectstatic
heroku run 'bower install --config.interactive=false;grunt prep;python manage.py collectstatic --noinput'
  1. 為未來的部署啟用集合
heroku config:unset DISABLE_COLLECTSTATIC
  1. 再次運行 collectstatic
heroku run python manage.py collectstatic

就像 Chris、phd 和 Selcuk 提到的那樣,我似乎忽略了我的 requirements.txt 文件中的一些必要部分。 我用了

    pip freeze

並再次復制/粘貼到 requirements.txt 文件中,這次它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM