Heroku-设置Procfile以运行Django命令来创建数据库

时间:2019-02-14 17:39:33

标签: django heroku

我正在将DjangoApp部署到Heroku。

我正在关注所有文档,但是不确定如何设置Procfile来运行命令,就像我在本地创建shop_peru DB一样:

这是我在本地创建秘鲁数据库的工作:

python manage.py ubigeo_peru

这是我的Procfile:

release: python manage.py makemigrations
release: python manage.py migrate
release: python manage.py ubigeo_peru
web: gunicorn stickers_gallito.wsgi --log-file -

ubigeo_peru.py:

import pandas as pd
import csv
from shop.models import Peru
from django.core.management.base import BaseCommand



tmp_data=pd.read_csv('static/data/ubigeo-peru-2018-12-25.csv',sep=',', encoding="utf-8")


class Command(BaseCommand):
    def handle(self, **options):
        products = [
            Peru(
                departamento=row['departamento'],
                provincia=row['provincia'],
                distrito=row['distrito'],
            )
            for idx, row in tmp_data.iterrows()
        ]

        Peru.objects.bulk_create(products)

在使用heroku时出现此错误:

    Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 795 bytes | 795.00 KiB/s, done.
Total 8 (delta 6), 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/core/management/__init__.py", line 204, in fetch_command
remote:            app_name = commands[subcommand]
remote:        KeyError: 'collectstatic'
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 375, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 211, in fetch_command
remote:            settings.INSTALLED_APPS
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 57, in __getattr__
remote:            self._setup(name)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 44, in _setup
remote:            self._wrapped = Settings(settings_module)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 107, in __init__
remote:            mod = importlib.import_module(self.SETTINGS_MODULE)
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 "/tmp/build_1d164976f07d1b5308e0df603fc5a5c0/stickers_gallito/settings.py", line 2, in <module>
remote:            import django_heroku
remote:        ModuleNotFoundError: No module named 'django_heroku'
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 stickers-gallito-app.
remote: 
To https://git.heroku.com/stickers-gallito-app.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/stickers-gallito-app.git'
(stickers_gallito_env) ogonzales@ogonzales:~/Desktop/web_proyects/stickers_gallito$ git push heroku master
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 1.07 KiB | 1.07 MiB/s, done.
Total 12 (delta 9), 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:        167 static files copied to '/tmp/build_48b84f7db1a8a5df5eb464b11621fa61/staticfiles', 67 unmodified, 278 post-processed.
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> release, web
remote: 
remote: -----> Compressing...
remote:        Done: 115.8M
remote: -----> Launching...
remote:  !     Release command declared: this new release will not be available until the command succeeds.
remote:        Released v17
remote:        https://stickers-gallito-app.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
remote: Running release command...
remote: 
remote: Traceback (most recent call last):
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote:     return self.cursor.execute(sql, params)
remote: psycopg2.ProgrammingError: relation "shop_peru" does not exist
remote: LINE 1: INSERT INTO "shop_peru" ("departamento", "provincia", "distr...
remote:                     ^
remote: 
remote: 
remote: The above exception was the direct cause of the following exception:
remote: 
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 375, in execute
remote:     self.fetch_command(subcommand).run_from_argv(self.argv)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
remote:     self.execute(*args, **cmd_options)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
remote:     output = self.handle(*args, **options)
remote:   File "/app/shop/management/commands/ubigeo_peru.py", line 22, in handle
remote:     Peru.objects.bulk_create(products)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
remote:     return getattr(self.get_queryset(), name)(*args, **kwargs)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 465, in bulk_create
remote:     ids = self._batched_insert(objs_without_pk, fields, batch_size)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1149, in _batched_insert
remote:     inserted_id = self._insert(item, fields=fields, using=self.db, return_id=True)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1136, in _insert
remote:     return query.get_compiler(using=using).execute_sql(return_id)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1289, in execute_sql
remote:     cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
remote:     return super().execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
remote:     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
remote:     return executor(sql, params, many, context)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote:     return self.cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
remote:     raise dj_exc_value.with_traceback(traceback) from exc_value
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
remote:     return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: relation "shop_peru" does not exist
remote: LINE 1: INSERT INTO "shop_peru" ("departamento", "provincia", "distr...
remote:                     ^
remote: 
remote: Waiting for release.... failed.
To https://git.heroku.com/stickers-gallito-app.git
   ab60b75..16f4a2f  master -> master

结构:

stickers_gallito (project)
   |_shop (app)
     |_management
       |_commands
         |_ubigeo_peru.py

设置:

WSGI_APPLICATION = 'stickers_gallito.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'ddl7v8vi4kdkip',
        'USER': 'u6sic8t2nrdejq',
        'PASSWORD': 'p72e949360dda1614aabf9a51b531d0c4a5f06ecc7e6528e8454f81687b7a321d',
        'HOST': 'ec2-54-157-94-8.compute-1.amazonaws.com',
        'PORT': '5432',

    }
}

1 个答案:

答案 0 :(得分:2)

您不能在Heroku中使用sqlite数据库。使用它们提供的数据库。 我通常使用postgresql

heroku addons:create heroku-postgresql:hobby-dev

这将为您创建一个空数据库

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# add this
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

这将更改您设置中的数据库。py