繁体   English   中英

Django ManifestStaticFilesStorage + DEBUG = False 在上传的文件中给出错误:他们不显示

[英]Django ManifestStaticFilesStorage + DEBUG = False gives error in uploaded files: They don't show

我有一个问题,我会尽力解释它,看看你是否能帮助我。

语境

我有一个在测试服务器中运行的系统,该系统设置为像生产环境一样,以便在合并到 master 之前测试我的代码,并将 go 提供给我的客户端的生产服务器提供商以更新代码。 这意味着它在DEBUG = False中运行。 一切都好,几个月都完美。

I decided to activate the ManifestStaticFilesStorage setting in order to have a hash number added in my static files, I've used it before and it's a good way to break cache rules when updating files (like CSS rules that refuse to load). 缓存存在一个问题,可以解决与服务器混淆的问题,但在这种情况下,这不是一个选项。

一切都很顺利:

  1. 除了一些丢失的 static 文件外,collectstatic 没有问题(已经解决)
  2. Static 文件加载完美

但...

问题

该系统管理内容(图像、音频文件和自定义字体)。 当我激活ManifestStaticFilesStorage设置时,所有上传的文件都开始在服务器访问日志中抛出 404 错误(偶尔会出现 500 错误)。 意思是,它们看起来像这样:

破碎的图像

您可以看到损坏的图像图标,但您也可以看到每个正方形的背景颜色(由 JS 注入的颜色,因为它可以在自定义 CMS 中自定义)。 这些图像在 CMS 中上传,它们位于设置文件中配置的媒体文件夹中。

当然,如果我 go 到DEBUG = True ,一切都会得到修复(来吧。-.-),我去并在本地重新创建生产环境:同样的问题: DEBUG = False bad, DEBUG = True工作

想法

  • 404 表示文件不存在。 你猜怎么着? 它在那里,它们都在
  • 偶尔的 500 表示权限。 好吧,我没有更改权限。 此外,权限相同。 另外,我正在使用 WebFaction,它为我处理所有这些
  • 浏览器中的控制台显示网络错误。 在“网络”选项卡中,它甚至不显示 404 错误或可能出现的少数 500 个错误
  • 损坏的数据库? 没有。 此外,如果它被破坏, DEBUG = True将失败
  • Apache 错误日志? 没有什么可显示的。 访问日志显示访问错误,即正确放置的文件上的错误

所以,我的想法用完了。 也许有人在那里有答案,我希望如此。 我仍然会尝试解决它,但我可以使用帮助,拜托。

相关代码

存储.py

class ManifestStaticFilesStorageNotStrict(ManifestStaticFilesStorage):
    """A relaxed implementation of django's ManifestStaticFilesStorage.
    """
    manifest_strict = False

settings.py(已编辑)

# -*- coding: utf-8 -*-

# Standard libs imports
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

# Django libs imports
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = <SECRET KEY HERE (: >

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

ALLOWED_HOSTS = ['*']

##########################
# APPLICATION DEFINITION #
##########################

DJANGO_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.sitemaps',
    'django.contrib.staticfiles',
]

THIRD_PARTY_APPS = [
    # https://github.com/audiolion/django-behaviors
    'behaviors.apps.BehaviorsConfig',
    # https://github.com/zostera/django-bootstrap4
    'bootstrap4',
    # https://django-ckeditor.readthedocs.io/en/latest/
    'ckeditor',
    # https://github.com/praekelt/django-recaptcha
    'captcha',
    # https://django-tables2.readthedocs.io/en/latest/index.html
    'django_tables2',
    # https://github.com/django-extensions/django-extensions
    'django_extensions',
]

CUSTOM_APPS = [
    <CUSTOM APPS HERE (: >
]

ELASTICSEARCH_DSL = {
    <ELASTICSEARCH DATA HERE (: >
}

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + CUSTOM_APPS

SITE_ID = 1

AUTH_USER_MODEL = 'users.User'

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

    # https://github.com/PaesslerAG/django-currentuser
    'django_currentuser.middleware.ThreadLocalUserMiddleware',
]

ROOT_URLCONF = 'main.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'main/templates')],
        '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',
                'main.context_processors.add_to_context',
            ],
        },
    },
]

DJANGO_TABLES2_TEMPLATE = 'django_tables2/bootstrap-responsive.html'

WSGI_APPLICATION = 'main.wsgi.application'

LOGIN_URL = reverse_lazy('back_office:auth:login')

SILENCED_SYSTEM_CHECKS = ['captcha.recaptcha_test_key_error']

#####################
# DATABASE SETTINGS #
#####################
#
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    <DATABASE DATA HERE (: >
}

##########################
# AUTHENTICATION BACKEND #
##########################
#
# https://docs.djangoproject.com/en/2.0/topics/auth/customizing/

AUTHENTICATION_BACKENDS = [
    <AUTHENTICATION_BACKENDS DATA HERE (: >
]

################################
# PASSWORD VALIDATION SETTINGS #
################################
#
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation'
                '.UserAttributeSimilarityValidator',
    },
    # {
    #     'NAME': 'django.contrib.auth.password_validation'
    #             '.MinimumLengthValidator',
    # },
    {
        'NAME': 'main.validators.password_validators'
                '.CustomMinimumLengthValidator',
    },
    # {
    #     'NAME': 'django.contrib.auth.password_validation'
    #             '.CommonPasswordValidator',
    # },
    {
        'NAME': 'main.validators.password_validators'
                '.CustomCommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation'
                '.NumericPasswordValidator',
    },
]

##################
# GOOGLE ANALYTICS
##################

GOOGLE_ANALYTICS_ID = ""


##################
# EMAIL SETTINGS #
##################

<SETTINGS HERE (: >

#################################
# INTERNATIONALIZATION SETTINGS #
#################################
#
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'es'

LANGUAGES = (
    ('en', _('English')),
    ('es', _('Spanish')),
)

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

#############################
# STATIC FILES SETTINGS     #
# (CSS, JavaScript, Images) #
#############################
#
# https://docs.djangoproject.com/en/1.11/howto/static-files/

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

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/media')

##############
# RECAPTCHA #
##############

<MORE SETTINGS HERE (: >

################
# FILE STORAGE #
################
#
# This settings is for a custom random number to add to all uploaded
# files in order to break cache

DEFAULT_FILE_STORAGE = 'main.storage.CustomFileSystemStorage'

##################
# LOCAL SETTINGS #
##################
#
# This is the file that contains local configurations like DB passwords,
# keys, user for an API, etc.
#
# This import is done at the end because it will override the default settings
# stablish here.

try:
    from .local_settings import *  # noqa
except Exception as e:
    pass

###################
# CKEDITOR CONFIG #
###################
CKEDITOR_CONFIGS = {
    'default': {
        # 'skin': 'moono',
        'toolbar': 'full',
        'skin': 'office2013',
        'width': '100%',
    }
}

########################
# STATIC FILES STORAGE #
########################
STATICFILES_STORAGE = \
    'main.storage.ManifestStaticFilesStorageNotStrict'

local_settings.py(已编辑)

# Production settings

DEBUG = False

###################
# ALLOWED DOMAINS #
###################

ALLOWED_HOSTS = [
    'localhost',
    'localhost:8000',
    '127.0.0.1',
    '127.0.0.1:8000',
    <DOMAIN DATA HERE (: >
]


#############
# DATABASES #
#############

DATABASES = {
    <DATABASE DATA HERE (: >
}


################
# STATIC FILES #
################

STATIC_ROOT = '<PATH TO SERVER STATIC FOLDER>'

MEDIA_ROOT = '<PATH TO SERVER STATIC FOLDER>/media'

提前致谢。

好吧,在我的头撞到墙上很多,睡个好觉和新鲜的想法之后,我发现了问题。 像往常一样,这是有史以来最愚蠢的事情。

如果您可以在settings.py中看到,则MEDIA_URL设置为/media/ 通常,这在 Apache 服务器中不会成为问题,但在 WebFaction 中,结果却是我头痛的罪魁祸首。

For those who don't know, WebFaction obliges you to create apps for everything: Python env, PHP env, Static env, WordPress, Joomla, etc. By creating an app, you have to assing a unique path inside the Website definition, which是在给定域下运行的一组应用程序,因此所有应用程序都具有同一域下的路径,并且您不必修改(在大多数情况下) httpd.conf文件。

我有一个用于 Django 代码的应用程序和另一个用于 static 文件的应用程序,位于域static/下。 local_settings.py中,您可以看到MEDIA_ROOT位于此 static 路径内。

在常规的 Apache 部署中,您只需将/media/ URL 分配给路径即可。 在 WebFaction 中,鉴于没有 static 应用程序用于使用media/MEDIA_URL变量在切换到DEBUG = False时必须具有static/media作为分配值。

请记住: DEBUG = False使 Django 停止提供 static 文件并将其完全留给部署的服务器。 因此,在DEBUG = True中,媒体 URL 并不重要,因为 Django 足够聪明,可以看到过去。 但是当我切换到DEBUG = False时,它自然而然地失败了。

愚蠢的我,但是,这就是我们学习的方式。

问候。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM