简体   繁体   English

将Django应用程序部署到Heroku

[英]Deploying Django app to Heroku

I am a beginner to Python/coding/web development, and am running into and error during deployment process. 我是Python /编码/ Web开发的初学者,并且在部署过程中遇到错误。

I coded a matchmaking app using Python/Django. 我使用Python / Django编写了一个配对应用程序。 I am attempting to deploy this app using Heroku. 我正在尝试使用Heroku部署此应用。 I followed all the directions in terms of setting up server, initializing Git repo, created Profile, Gunicorn, etc. etc. etc. 我按照所有指示进行设置服务器,初始化Git存储库,创建Profile,Gunicorn等等等。

I was able to git push heroku master . 我能够git push heroku master

However, when I actually try to sync my files into the database, it returns an error. 但是,当我实际尝试将文件同步到数据库时,它返回错误。 I typed this: heroku run python manage.py make migrations . 我输入了这个: heroku run python manage.py make migrations

I get the following error: 我收到以下错误:

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...
                                                         ^

What does it mean by auth_user does not exist? auth_user不存在是什么意思? Does this mean I need to create a superuser? 这是否意味着我需要创建一个超级用户? I tried and it won't let me. 我试过了,它不会让我。 When I do heroku run python manage.py createsuperuser , it gives me the exact same error. 当我做heroku run python manage.py createsuperuser ,它给了我完全相同的错误。

Here is my models.py for Posting app 这是我的发布应用程序的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']

Also, not sure if this will be of any help, but I did a search for clean_username and based on my readings from this Django custom user model in admin, relation "auth_user" does not exist 另外,不确定这是否有帮助,但是我搜索了clean_username并基于我在admin中从此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

It seems you didn't migrate, can you try: 看来您没有迁移,可以尝试:

heroku run python manage.py migrate

And you don't have to heroku run python manage.py makemigrations since migration scripts are already there 而且您不必heroku run python manage.py makemigrations因为迁移脚本已经存在

try to run these two commands 尝试运行这两个命令

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

I was stuck in the exact same problem for more than 4 days. 我被完全相同的问题困扰了4天以上。 Everything got fixed with me when I used Postgres instead of Sqlite, which you are presumably using because it's the default option that comes with django, so I recommend following this tutorial to use Postgres: Django Girls: Installing PostgreSQL 当我使用Postgres而不是Sqlite时,一切都已经解决了,您大概会使用它,因为它是django附带的默认选项,因此,我建议按照本教程使用Postgres: Django Girls:安装PostgreSQL

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM