【问题标题】:deploying bootstrap on heroku with django使用 django 在 heroku 上部署引导程序
【发布时间】:2017-12-24 20:56:54
【问题描述】:

我在 Heroku 客户端上部署 twitter 引导程序时遇到问题。我的 html 代码显示不正常(对齐、特殊表格布局等)

一切都在本地工作,但我认为这与未正确传输的静态文件有关。

这是静态文件结构,但是看看我在我的 live heroku 版本上打开“源 chrome 控制台”时的样子。

文件夹结构的视图

heroku上部署代码的文件夹结构

看起来有些文件夹是“缝合”到另一个的。我的问题可能是什么?我应该避免在我的文件名中使用“-”和“_”吗?

感谢您的帮助。

基于额外的问题,我在我的 settings.py 文件中添加了更多信息。我按照一个教程把所有的东西都做好了。还安装了 whitenoise,添加到 wsgi.py。

wsgi.py

"""
WSGI config for xforward project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xforward.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

settings.py

--

"""
Django settings for xforward project on Heroku. For more info, see:
https://github.com/heroku/heroku-django-template

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os
import dj_database_url

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "{{ secret_key }}"

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

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.admindocs', ### onbekend of deze nodig is
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    # Disable Django's own staticfiles handling in favour of WhiteNoise, for
    # greater consistency between gunicorn and `./manage.py runserver`. See:
    # http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    'frontendapp',
]

LOGIN_REDIRECT_URL = 'forwardthis'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'xforward.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG,
        },
    },
]

WSGI_APPLICATION = 'xforward.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get('postgresname'),
        'USER': os.environ.get('postgresuser'),
        'PASSWORD': os.environ.get('postgrespassword'),
        'HOST': os.environ.get('postgreshost'),
        'PORT': os.environ.get('postgresport'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

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

# Allow all host headers
ALLOWED_HOSTS = ['*']

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

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

--

【问题讨论】:

  • 所以这不是您可能知道的关于 heroku 上的静态文件的第一篇文章。除了关于这个主题的数百个问题之外,还有文档。你应该关注其中之一。您的 settings.py 在哪里,请发布。 Django Whitenoise 安装了吗?你运行 collectstatic 和配置的 url?您更改了 static_url 并使用 git 正确配置了所有内容?不?这样做!
  • 您好 hansTheFranz,感谢您的快速回复。我遵循了一些教程,安装了 whitenoise 并配置了 settings.py(在问题中添加了这个和 wsgi.py - 抱歉没有提到这个,初学者说话)。我跑了collectstatic,也在heroku端。我不知道您所说的“配置的 url 是什么意思?您更改了 static_url 并且所有这些都使用 git 正确配置了?不?这样做!”;感谢您的帮助!
  • 那是我在 heroku 上运行应用程序时遇到问题时的帖子:stackoverflow.com/questions/44835979/…。将其作为清单所有这些设置也应该在您的应用程序中,但我在您的 settings.py 中看到您拥有大部分设置。配置的 url 意味着您需要一个指向静态文件的 url。您已经更改了 static_url ,这很好,但您的 STATIC_ROOT 是错误的。 Heroku 什么也找不到!它需要只是“静态的”。您的所有文件(包括正确格式的静态文件)都需要使用 git 上传。
  • 运行 collectstatic 时会收到什么消息?还要为您的项目初始化日志记录,以便您可以转到 Heroku 日志并检查问题所在。
  • 等等。你有 debug = true 并在生产中运行它?它根本不保存!您很容易被黑客入侵,以后不要这样做。但是非常奇怪的是,当你有 debug=true 时,你的静态文件不会被加载,因为 Django 也会在 heroku 上处理这个问题。请发布您如何在模板中引用文件。 <link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}" type="text/css"/> 喜欢这样吗?

标签: css django twitter-bootstrap heroku django-staticfiles


【解决方案1】:

在与 hansTheFranz 进行彻底讨论后,很明显问题与丢失的文件有关。

我用于与 github 同步的 sourcetree 应用程序中使用的默认 gitnore 阻止了几个静态文件夹上传到我的 git。从这个 gitnore 文件中删除这些文件夹就可以了。

感谢大家的帮助!

【讨论】:

  • 很高兴我能帮上忙。我认为您在此过程中学到了很多东西,这很重要。虽然我们第一次部署时都会这样做,但最后你学到了很多东西并且知道了问题所以我希望我能很快看到你帮助遇到同样问题的人:)
【解决方案2】:

转到 Django 的 heroku 静态资产文档:

Heroku Documentation: Django and Static Assets

那里详细说明了所有内容,在settings.py中进行更改并安装whitenoise,它将起作用。

【讨论】:

  • 完全遵循本指南。但没有成功的结果。
  • @radzia2 检查您的 STATIC_URL 是否正确,因为 css、js 和所有这些文件都是静态的,并且您还有一层目录-frontagenewagetheme
猜你喜欢
  • 2011-12-05
  • 2015-06-30
  • 1970-01-01
  • 2018-05-15
  • 2013-08-22
  • 1970-01-01
  • 2011-12-19
  • 2018-02-09
  • 2016-04-15
相关资源
最近更新 更多