当 debug = false 在我的 django 网络应用程序

问题描述

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

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

我的问题是,当我的调试设置为 true 并且我尝试 runserver 时,一切正常,但是当我将其更改为 False 并尝试 runserver 时,出现 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,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'),'sidebar': {
        'name': gettext('Sidebar'),'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',}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)