当我尝试从我的heroku应用程序访问我的消息时收到ProgrammingError。它在我的本地计算机上运行良好。我错过了什么吗?

时间:2017-03-07 16:20:02

标签: python django heroku

Environment:


Request Method: GET
Request URL: https://my-trial-project.herokuapp.com/messages/

Django Version: 1.10.1
Python Version: 2.7.13
Installed Applications:
[

    u'django.contrib.admin',
     u'django.contrib.auth',
     u'django.contrib.contenttypes',
     u'django.contrib.sessions',
     u'django.contrib.messages',
     u'django.contrib.staticfiles',
     u'Trial_app',
     u'authentication',
     u'feeds',
     u'user_profile',
     u'user_messages',
     u'debug_toolbar'

]
Installed Middleware:
[



    u'django.middleware.security.SecurityMiddleware',
     u'django.contrib.sessions.middleware.SessionMiddleware',
     u'django.middleware.common.CommonMiddleware',
     u'django.middleware.csrf.CsrfViewMiddleware',
     u'django.contrib.auth.middleware.AuthenticationMiddleware',
     u'django.contrib.messages.middleware.MessageMiddleware',
     u'django.middleware.clickjacking.XFrameOptionsMiddleware',
     u'debug_toolbar.middleware.DebugToolbarMiddleware'

]


Traceback:

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/app/user_messages/views.py" in user_messages
  58.     msg_to_user = UserMessages.get_sent_messages(current_user)

File "/app/user_messages/models.py" in get_sent_messages
  34.         msgs = UserMessages.objects.read_message(message_data)

File "/app/user_messages/models.py" in read_message
  10.         for _id in message_id:

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  256.         self._fetch_all()

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  155.         for row in compiler.results_iter():

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  789.             results = self.execute_sql(MULTI)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /messages/
Exception Value: column user_messages_usermessages.user_to_id does not exist
LINE 1: ...sages_id" FROM "user_messages_usermessages" WHERE "user_mess...
                                                             ^
HINT:  Perhaps you meant to reference the column "user_messages_usermessages.user_id".

Views.py

from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_http_methods
from user_messages.forms import MessagesForm
from user_messages.models import UserMessages
from django.contrib.auth.models import User


# for displaying message forms
@login_required(login_url="login")
def user_messages_form(request):
    if request.method == "POST":
        messages = MessagesForm(request.POST)
        if messages.is_valid():
            user_from = request.user
            user_to = messages.cleaned_data.get("message_to")
            user_to_obj = User.objects.get(username=user_to)
            user_message = messages.cleaned_data.get("message")

            UserMessages.create_message(user_from, user_to_obj, user_message)
            return redirect("/messages/")
        else:
            return render(request, "user_messages/message_form.html", {
                "messages": messages,
            })
    else:
        messages = MessagesForm()
    return render(request, "user_messages/message_form.html", {
        "messages": messages,
    })



# displays user messages
@login_required(login_url="login")
def user_messages(request):
    current_user = request.user
    # unread = UserMessages.get_unread_messages_(current_user)
    msg_to_user = UserMessages.get_sent_messages(current_user)
    msg_from_user = UserMessages.get_received_messages(current_user)

    return render(request, "user_messages/message.html", {
        "msg_from_user": msg_from_user,
        "current_user": current_user,
        "msg_to_user": msg_to_user,
        # "unread": unread,
    })

Models.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.encoding import python_2_unicode_compatible


class UserMessagesManager(models.Manager):
    def read_message(self, message_id):
        result = []
        for _id in message_id:
            message = super(UserMessagesManager, self).get(pk=_id)
            message.is_read = True
            message.save()
            result.append(message)
        return result


@python_2_unicode_compatible
class UserMessages(models.Model):
    objects = UserMessagesManager()
    user_messages_id = models.AutoField(primary_key=True)
    user_to = models.ForeignKey(User, related_name="+")
    user_from = models.ForeignKey(User, related_name="+")
    user_messages = models.CharField(max_length=500)
    user_messages_timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
    is_read = models.BooleanField(default=False)

    def __str__(self):
        return str(self.user_messages_timestamp) + "\t" + self.user_messages + "\t" + str(self.is_read)

    @staticmethod
    def get_sent_messages(user):
        message_data = UserMessages.objects.filter(user_to=user).values_list("user_messages_id", flat=True)
        msgs = UserMessages.objects.read_message(message_data)
        return msgs

    @staticmethod
    def get_unread_messages_(user):
        unread_messages = UserMessages.objects.filter(user_to=user).filter(is_read=False)
        return unread_messages

    @staticmethod
    def get_received_messages(user):
        message_data = UserMessages.objects.filter(user_from=user)
        return message_data

    @staticmethod
    def create_message(from_user, to_user, message):
        UserMessages.objects.create(
            user_from=from_user,
            user_to=to_user,
            user_messages=message,
        )

    class Meta:
        ordering = ["-user_messages_timestamp"]

0 个答案:

没有答案