简体   繁体   中英

Deploying Django app to Heroku

I am a beginner to Python/coding/web development, and am running into and error during deployment process.

I coded a matchmaking app using Python/Django. I am attempting to deploy this app using Heroku. I followed all the directions in terms of setting up server, initializing Git repo, created Profile, Gunicorn, etc. etc. etc.

I was able to 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 .

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

Here is my models.py for Posting app

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

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

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

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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