簡體   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