Django TemplateDoesNotExist at /debug_toolbar/base.html 部署到 EC2 后

Posted

技术标签:

【中文标题】Django TemplateDoesNotExist at /debug_toolbar/base.html 部署到 EC2 后【英文标题】:Django TemplateDoesNotExist at / debug_toolbar/base.html after deployiing to EC2 【发布时间】:2019-07-12 13:54:59 【问题描述】:

我正在尝试在 EC2 实例上部署 Django 应用程序。我已正确地将我的 EC2 实例放入 Allowed_Host 中。但它一直给我一个 Disallowed_Host 错误。我一直在努力解决这个问题,但现在我明白了。

Django TemplateDoesNotExist at /debug_toolbar/base.html 错误

谁能告诉我我做错了什么?下面是我的截图和代码。顺便说一句,它在我的开发 127.0.0.1 中运行得非常好

下面还有 Traceback 以防万一

我在下面附上了我的 settings.py 文件

import os


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')


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

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

ALLOWED_HOSTS = ["ec2-11-111-111-11.compute-1.amazonaws.com", "127.0.0.1", "Siteevent.com", "www.Siteevent.com"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.gis',
    'bootstrap3',
    'accounts',
    'groups',
    'posts',
    'proof',
    'feeds',
    'questions',
    'tasting',
    'verification',
    'cart',
    'stripe',
    'order',
    'report',
    'django_cleanup',
    'bootstrap_datepicker_plus',
    'social_django',
    'storages',
    'captcha',


]

AUTHENTICATION_BACKENDS = [

    'social_core.backends.google.GoogleOAuth2',
    'social_core.backends.facebook.FacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
    'accounts.authentication.EmailAuthBackend',

]


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',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

ROOT_URLCONF = 'Site.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, os.path.join(BASE_DIR, 'order', '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',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
                'cart.context_processors.counter',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'Site.wsgi.application'


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

DATABASES = 
    'default': 
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'Site_event',
        'USER': '11111111111111111111111',
        'PASSWORD': '11111111111111111111111111',
        'HOST': 'Site-event1111111111111111111111111rds.amazonaws.com',
        'PORT': '5432',
    



# Password validation
# 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': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    ,
]


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

LANGUAGE_CODE = 'en-us'

# TIME_ZONE = 'UTC'
TIME_ZONE = "America/New_York"
USE_TZ = True

USE_I18N = True

USE_L10N = True

AWS_DEFAULT_ACL = None
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
DEFAULT_FILE_STORAGE = 'aws_storage_classes.MediaStorage'
AWS_STORAGE_BUCKET_NAME = 'Site-media-1111111111111'
STATICFILES_STORAGE = 'aws_storage_classes.StaticStorage'
AWS_S3_OBJECT_PARAMETERS = 
    'CacheControl': 'max-age=86400',


AWS_S3_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME


STATIC_URL = 'https://%s/static/' % AWS_S3_DOMAIN
MEDIA_URL = 'https://%s/media/' % AWS_S3_DOMAIN

ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'


LOGIN_REDIRECT_URL = 'loggedin'
LOGOUT_REDIRECT_URL = 'accounts:login'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '1111111111111111111111111111111111111111111'
SOCIAL_AUTH_GOOGLE_OAUTH_SECRET = '111111111111111111111111111111111111111111'


INTERNAL_IPS = ('127.0.0.1', )

BOOTSTRAP3 = 
    'include_jquery': True,


DATE_INPUT_FORMATS = ['%b %d %Y']

TIME_INPUT_FORMATS = ['%I:%M %p']

####Stripe Settings###

STRIPE_PUBLISHABLE_KEY = '11111111111111111111111111111111111'
STRIPE_SECRET_KEY = '11111111111111111111111111111111111'


RECAPTCHA_PUBLIC_KEY = "11111111111111111111111111111111111111111111"
RECAPTCHA_PRIVATE_KEY = "111111111111111111111111111111"


# try:
#     from Site.local_settings import *
# except ImportError as e:
#     pass
#

将 django_toolbar 添加到 INSTALED_APPS 会出现以下错误

我什至尝试删除 django_toolbar 和所涉及的中间层,这在不同的地方给了我同样的错误

【问题讨论】:

【参考方案1】:

发生错误是因为您已将 debug_toolbar 包含在中间件中,但未包含在 INSTALLED_APPS 中。所以要么你需要从MIDDLEWARE中删除'debug_toolbar.middleware.DebugToolbarMiddleware',

debug_toolbar 添加到INSTALLED_APPS。参考django_toolbar。

【讨论】:

我尝试了你的两个建议我得到一个No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials 错误见上图 @MarcoBianchi 我想你可以看看这个:***.com/questions/41201422/… 感谢您的解决方案解决了我的 debug_toolbar 问题【参考方案2】:

我遇到了同样的问题。

但对我来说,这是由 INTERNAL_IPS 引起的。

当它设置为:INTERNAL_IPS = ['127.0.0.1', ] 并尝试访问 http://localhost:8000/ 时不起作用,IP 地址也不行。

但是,如果它变成:INTERNAL_IPS = ['localhost', ],它可以与http://127.0.0.1:8000/ 和 http://localhost:8000/ 一起正常工作。

【讨论】:

你真的看到工具栏了吗?在我的情况下,'localhost' 只是禁用了它。

以上是关于Django TemplateDoesNotExist at /debug_toolbar/base.html 部署到 EC2 后的主要内容,如果未能解决你的问题,请参考以下文章

Django初识

Django之路

Django系列

django 错误

mac电脑安装django ,运行django报错解决

Django 大神带你飞系列~走进Django