【问题标题】:Static files in django 1.5 - working locally, not working on Herokudjango 1.5 中的静态文件 - 在本地工作,而不是在 Heroku 上工作
【发布时间】:2014-03-20 15:39:29
【问题描述】:

我在 Django 1.5 中有一个项目并将其托管在 Heroku 上。 问题是我没有看到服务器静态文件(CSS,JS)(在用户和管理员上) - 浏览了可用的教程:https://devcenter.heroku.com/articles/django-assets 但没有任何帮助。

当我使用 python manage.py runserver 在本地启动应用程序时 - 一切正常。下面是文件setting.py中的代码

    import dj_database_url

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

    # 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: don't run with debug turned on in production!
    DEBUG = False

    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',
        'app',
    )

    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 = 'blog_project.urls'

    WSGI_APPLICATION = 'blog_project.wsgi.application'


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

    DATABASES = {'default': dj_database_url.config()}


    # 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/'
    STATIC_ROOT = 'staticfiles'

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

【问题讨论】:

    标签: python django heroku hosting django-staticfiles


    【解决方案1】:

    我认为您需要更改 Heroku 的设置,以便包含

    PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
    STATICFILES_DIRS = (
        os.path.join(PROJECT_PATH, 'static'),
    )    
    

    它使用不同的查找方式

    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    

    【讨论】:

    • 这解决了问题:from dj_static import Cling application = Cling(get_wsgi_application()) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多