【问题标题】:Getting started with django and heroku, "application error" after uploaddjango和heroku入门,上传后出现“应用程序错误”
【发布时间】:2014-07-23 04:57:33
【问题描述】:
  1. 我一直在使用这个指南:https://devcenter.heroku.com/articles/getting-started-with-django

requirements.txt

Django==1.6.5
argparse==1.2.1
dj-database-url==0.3.0
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.3
pystache==0.5.4
static==1.0.2
wsgiref==0.1.2

文件结构

   hellodjango
   ├── hellodjango
   │   ├── __init__.py
   │   ├── settings.py
   │   ├── settings.py~
   │   ├── urls.py
   │   ├── wsgi.py
   │   └── wsgi.py~
   └── manage.py
   1 directory, 7 files

目录 Projects/projects/6.2.2014 中的其他文件

    (venv)user@user-VirtualBox:~/Projects/projects/6.2.2014$ ls
    hellodjango  Procfile  Procfile~  requirements.txt  venv

heroku 日志中的 sn-p:

    2014-06-02T23:42:49.621172+00:00 app[web.1]: ImportError: No module named  
    hellodjango.wsgi
    2014-06-02T23:42:49.621171+00:00 app[web.1]:     __import__(module)
    2014-06-02T23:42:49.621219+00:00 app[web.1]: 2014-06-02 23:42:49 [7] [INFO] Worker
    exiting (pid: 7)
    2014-06-02T23:42:49.790790+00:00 app[web.1]: 2014-06-02 23:42:49 [2] [INFO] Shutting
    down: Master
    2014-06-02T23:42:51.344028+00:00 heroku[web.1]: Process exited with status 3
    2014-06-02T23:42:47.298345+00:00 heroku[web.1]: Starting process with command 
    `gunicorn hellodjango.wsgi`
    2014-06-02T23:42:51.355014+00:00 heroku[web.1]: State changed from starting to 
    crashed
    (venv)user@user-VirtualBox:~/Projects/projects/6.2.2014$ 

Procfile

    web: gunicorn hellodjango.wsgi -b 0.0.0.0:$PORT

谁能指出我在这里所缺少的方向? 当 Procfile 只有这个时,我在访问我的网站后遇到了同样的“应用程序错误”:

Procfile(旧)

    web: gunicorn hellodjango.wsgi

设置文件

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    import os
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    # I removed this line for the stack overflow posting SECRET_KEY = ''

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    TEMPLATE_DEBUG = True

    ALLOWED_HOSTS = ['*']


    # Application definition

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'gunicorn', #this wasn't here before, i added it and still got the application 
         # error
        )

    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
       'django.middleware.clickjacking.XFrameOptionsMiddleware',
     )

    ROOT_URLCONF = 'hellodjango.urls'

    WSGI_APPLICATION = 'hellodjango.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/1.6/ref/settings/#databases

    """
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
     }
    """
    # Internationalization
    # https://docs.djangoproject.com/en/1.6/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True


    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.6/howto/static-files/

    STATIC_URL = '/static/'

    # Parse database configuration from $DATABASE_URL
    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()

    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')


    # Static asset configuration
    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/static/'

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )

这是我当前在 /Projects/projects/6.2.2014/hellodjango/hellodjango 中的 wsgi.py 文件

    import os
    from django.core.wsgi import get_wsgi_application
    from dj_static import Cling
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")


    application = Cling(get_wsgi_application())

【问题讨论】:

  • 您的 wsgi 文件是什么样的?您的 requirements.txt 文件也不在您的项目结构中吗?这只是一个简单的错误还是没有检查到您的 git 存储库中?
  • 刚刚添加。你的意思是 wsgi.py 文件,对吧? requirements.txt 文件与以下文件夹位于同一文件夹中:{hellodjango Procfile Procfile~ requirements.txt venv}。它是在我运行“pip freeze > requirements.txt”时自动创建的

标签: python django heroku


【解决方案1】:

将您的 wsgi.py 文件更改为:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
from dj_static import Cling
application = Cling(get_wsgi_application())

你需要在导入 Cling 之前指定你的设置,否则会出错。

【讨论】:

  • 谢谢你解决了我的问题!
【解决方案2】:

将此添加到您的 .wsgi 文件中

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

【讨论】:

  • 感谢您的帮助!我想我的 wsgi.py 文件中已经有了它。我继续编辑了原始帖子,以便每个人都可以看到我当前的 wsgi.py 文件。
  • 确保你从项目的基础目录推送到heroku
  • 你是说 wsgi.py 吗?
  • 在我的情况下,基本目录在文件夹“6.2.2014”中,对吧?
  • 不,我的意思是当你通过“git push heroku master”推送你的应用程序时,从 .../6.2.2014/hellodjago 给出这个命令,即你的 manage.py 所在的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 2015-01-08
  • 2021-12-06
  • 2021-11-27
  • 2017-07-08
  • 2023-03-24
相关资源
最近更新 更多