Django和Heroku入门,上传后出现“应用错误”

1 投票
2 回答
3825 浏览
提问于 2025-04-18 08:21
  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 日志的一段信息:

    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())

2 个回答

1

把这个添加到你的 .wsgi 文件里

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

application = Cling(get_wsgi_application())
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 之前先指定你的设置,不然会出错。

撰写回答