将Django app部署到Heroku

2024-04-18 03:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python/coding/web开发的初学者,在部署过程中遇到了错误。在

我用Python/Django编写了一个婚介应用程序。我正在尝试使用Heroku部署此应用程序。在设置服务器、初始化Git repo、创建Profile、Gunicorn等方面,我遵循了所有的指导

我能够git push heroku master。在

但是,当我实际尝试将文件同步到数据库时,它会返回一个错误。我输入了这个:heroku run python manage.py make migrations。在

我得到以下错误:

Running python manage.py migrate on ⬢ blooming-island-78995... up, run.1306
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
    django.setup()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/app/directmessages/models.py", line 9, in <module>
    user_obj = User.objects.get(username='ayaspencer') 
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 381, in get
    num = len(clone)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 240, in __len__
    self._fetch_all()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
                                                         ^

授权用户不存在是什么意思?这是否意味着我需要创建一个超级用户?我试过了,但它不让我这么做。当我做heroku run python manage.py createsuperuser时,它会给我同样的错误。在

这是我的模型.py用于发布应用程序

^{pr2}$

另外,不确定这是否有任何帮助,但我搜索了clean\u用户名,并基于我从这个Django custom user model in admin, relation "auth_user" does not exist中读到的信息

python2.7/site-packages/django/contrib/auth/backends.py:

  123              return
  124          user = None
  125:         username = self.clean_username(remote_user)
  126  
  127          UserModel = get_user_model()
  ...
  143          return user
  144  
  145:     def clean_username(self, username):
  146          """
  147          Performs any cleaning on the "username" prior to using it to get or

python2.7/site-packages/django/contrib/auth/middleware.py:

   78          # persisted in the session and we don't need to continue.
   79          if request.user.is_authenticated():
   80:             if request.user.get_username() == self.clean_username(username, request):
   81                  return
   82              else:
   ..
   94              auth.login(request, user)
   95  
   96:     def clean_username(self, username, request):
   97          """
   98          Allows the backend to clean the username, if the backend defines a
   99:         clean_username method.
  100          """
  101          backend_str = request.session[auth.BACKEND_SESSION_KEY]
  102          backend = auth.load_backend(backend_str)
  103          try:
  104:             username = backend.clean_username(username)
  105:         except AttributeError:  # Backend has no clean_username method.
  106              pass
  107          return username

7 matches across 2 files

Tags: djangoinpyselfappexecuteherokumodels
3条回答

试着运行这两个命令

heroku run python manage.py migrate auth
heroku run python manage.py migrate

我被同样的问题困扰了4天多。当我使用Postgres而不是Sqlite时,一切都得到了修复,您可能正在使用它,因为它是django附带的默认选项,所以我建议按照本教程使用Postgres:Django Girls: Installing PostgreSQL

祝你好运!在

您似乎没有迁移,您可以尝试一下:

heroku run python manage.py migrate

而且您不必heroku run python manage.py makemigrations,因为迁移脚本已经存在

相关问题 更多 >