首页 > 解决方案 > 当我的 django web 应用程序中的 debug = false 时,sekizai for adsense 出错

问题描述

我正在尝试将 google adsense 添加到我的 django 网络应用程序中,但由于我的网站上没有内容,我无法激活我的帐户。(啊,这一刻我只放了谷歌在我创建帐户时给我的脚本标签)。

所以我尝试将 django 广告和 sekizai 添加到我的应用程序中,以使 adsense 能够在其上运行。

我的问题是,当我的调试设置为真并且我尝试运行服务器时,一切正常,但是当我将其更改为 False 并尝试运行服务器时,我遇到了 500 错误,我不明白为什么......

在 sekizai 设置中我可能不太了解的一件事是最后一步:

“对于 1.10 之后的 Django 版本,将 sekizai.context_processors.sekizai 添加到您的 TEMPLATES['OPTIONS']['context_processors'] 设置中,并在渲染模板时使用 django.template.RequestContext。”

我应该把“django.template.RequestContext”放在哪里?这是我必须解决我的问题的唯一方法。

我给你留下了我的 settings.py 的几个部分,

感谢您的帮助和回答

设置.py:

"""
Django settings for yufindRe project.

Generated by 'django-admin startproject' using Django 3.2.

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

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

from pathlib import Path
import os
import dj_database_url

# Build paths inside the project like this: BASE_DIR / 'subdir'.
from ads.conf import gettext

BASE_DIR = Path(__file__).resolve().parent.parent


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret'

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

DEBUG = False


ALLOWED_HOSTS = ['test.herokuapp.com', '127.0.0.1']


# Application definition

INSTALLED_APPS = [

    'whitenoise.runserver_nostatic',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ask.apps.AskConfig',
    'contact.apps.ContactConfig',
    'home.apps.HomeConfig',
    'category.apps.CategoryConfig',
    'music.apps.MusicConfig',
    'streaming.apps.StreamingConfig',
    'videos.apps.VideosConfig',
    'vote.apps.VoteConfig',
    'about.apps.AboutConfig',
    'ads.apps.AdsConfig',
    'sekizai',

]

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 = 'yufindRe.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',
                'sekizai.context_processors.sekizai',
            ],
        },
    },
]

WSGI_APPLICATION = 'yufindRe.wsgi.application'



# Internationalization
# https://docs.djangoproject.com/en/3.2/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/3.2/howto/static-files/

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



#if os.environ.get('ENV') == 'PRODUCTION':

    # Static files settings
INTERNAL_IPS = ['127.0.0.1']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')


    # whitenoise static config
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


#ads config

ADS_GOOGLE_ADSENSE_CLIENT = None  # 'ca-pub-xxxxxxxxxxxxxxxx'

ADS_ZONES = {
    'header': {
        'name': gettext('Header'),
        'ad_size': {
            'xs': '720x150',
            'sm': '800x90',
            'md': '800x90',
            'lg': '800x90',
            'xl': '800x90'
        },
        'google_adsense_slot': None,  # 'xxxxxxxxx',
        'google_adsense_format': None,  # 'auto'
    },
    'content': {
        'name': gettext('Content'),
        'ad_size': {
            'xs': '720x150',
            'sm': '800x90',
            'md': '800x90',
            'lg': '800x90',
            'xl': '800x90'
        },
        'google_adsense_slot': None,  # 'xxxxxxxxx',
        'google_adsense_format': None,  # 'auto'
    },
    'sidebar': {
        'name': gettext('Sidebar'),
        'ad_size': {
            'xs': '720x150',
            'sm': '800x90',
            'md': '800x90',
            'lg': '800x90',
            'xl': '800x90'
        }
    }
}

ADS_DEFAULT_AD_SIZE = '720x150'





ADS_DEVICES = (
    ('xs', ('Extra small devices')),
    ('sm', ('Small devices')),
    ('md', ('Medium devices (Tablets)')),
    ('lg', ('Large devices (Desktops)')),
    ('xl', ('Extra large devices (Large Desktops)')),
)

ADS_VIEWPORTS = {
    'xs': 'd-block img-fluid d-sm-none',
    'sm': 'd-none img-fluid d-sm-block d-md-none',
    'md': 'd-none img-fluid d-md-block d-lg-none',
    'lg': 'd-none img-fluid d-lg-block d-xl-none',
    'xl': 'd-none img-fluid d-xl-block',
}

标签: pythondjangodjango-templatesadsensedjango-sekizai

解决方案


推荐阅读