0

我是 Python/编码/Web 开发的初学者,在部署过程中遇到错误。

我使用 Python/Django 编写了一个配对应用程序。我正在尝试使用 Heroku 部署此应用程序。我按照设置服务器、初始化 Git 存储库、创建 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...
                                                         ^

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

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

from django.db import models
from django.contrib.auth.signals import user_logged_in
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

def upload_location(instance, filename):
    #extension = filename.split(".")[1]
    location = str(instance.user.username)
    return "%s/%s" %(location, filename)


class PostingMessageManager(models.Manager):
    def get_num_unread_messages(self, user):
        return super(PostingMessageManager, self).filter(read=False).count()




class PostMessage(models.Model):
    subject = models.CharField(max_length=150)
    body = models.CharField(max_length=3000)
    service_being_requested = models.CharField(max_length=3000, null=True)
    service_being_offered = models.CharField(max_length=3000, null=True)
    sender = models.ForeignKey(User, related_name='sent_post_messages', null=True, blank=True)
    receiver = models.ForeignKey(User, related_name='received_post_messages', null=True, blank=True)
    sent = models.DateTimeField(auto_now_add=False, auto_now=False, null=True, blank=True)
    read_at = models.DateTimeField(auto_now_add=False, auto_now=False, null=True, blank=True)
    read = models.BooleanField(default=False)
    parent = models.ForeignKey('self', related_name='parent_message', null=True, blank=True)
    replied = models.BooleanField(default=False)
    CERTIFIED = 'Yes'
    NONCERTIFIED = 'No'
    INDIFFERENT = 'Indifferent'

    CERTIFICATION_CHOICES = (
        (CERTIFIED, 'Yes'),
        (INDIFFERENT,'Indifferent'),

    )

    CERTIFICATION_CHOICES_ME = (
        (CERTIFIED, 'Yes'),
        (NONCERTIFIED, 'No'),

    )
    should_they_be_certified = models.CharField(max_length=200,
                                      choices=CERTIFICATION_CHOICES,
                                      default=INDIFFERENT)

    are_you_certified = models.CharField(max_length=200,
                                      choices=CERTIFICATION_CHOICES_ME,
                                      default=NONCERTIFIED) 

    def is_certified(self):
        return self.should_they_be_certified in (self.CERTIFIED)

    def dont_care(self):
        return self.are_you_certified in (self.INDIFFERENT)

    def iam_certified(self):
        return self.are_you_certified in (self.CERTIFIED)


    def __unicode__(self):
        return self.body

    objects = PostingMessageManager()



    def get_absolute_url(self):
        return (reverse('view_post_message', kwargs={'ps_id': self.id}))

    class Meta:
        ordering = ['-sent',]



def set_messages_in_session(sender, user, request, **kwargs):
    post_message = PostMessage.objects.get_num_unread_messages(user)
    request.session['post_num_of_messages'] = post_message

user_logged_in.connect(set_messages_in_session)


#class F(models.Model):
    #certification = models.CharField(max_length=50, choices=CERTCHOICE)
    #class Meta:
        #model = PostMessage
        #fields = ['certification']

另外,不确定这是否有任何帮助,但我搜索了 clean_username 并根据我在管理员中从这个 Django 自定义用户模型中的读数,关系“auth_user”不存在

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
4

3 回答 3

2

看来你没有迁移,你可以试试:

heroku run python manage.py migrate

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

于 2016-07-14T02:29:29.363 回答
1

尝试运行这两个命令

heroku run python manage.py migrate auth
heroku run python manage.py migrate
于 2016-07-14T06:18:19.773 回答
0

我被困在完全相同的问题上超过 4 天。当我使用 Postgres 而不是 Sqlite 时,一切都得到了解决,您可能正在使用它,因为它是 django 附带的默认选项,所以我建议按照本教程使用 Postgres:Django Girls:安装 PostgreSQL

祝你好运!

于 2016-12-04T20:53:11.240 回答