gpt4 book ai didi

python - DEBUG=FALSE 时 Django 500 错误,但仅在某些页面上

转载 作者:行者123 更新时间:2023-12-01 03:06:06 32 4
gpt4 key购买 nike

我被困住了。当我设置我的 DEBUG=False ,我收到 500 错误,但仅当我单击转到我的 index.html 导航栏上的另一个应用程序的选项卡时.

这是我的设置.py:

"""
Django settings for Tanta project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_dir=os.path.join(BASE_DIR,"templates")
STATIC_DIR=os.path.join(BASE_DIR,"static")
MEDIA_DIR=os.path.join(BASE_DIR,'media')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
COMMUNITY_DIR=os.path.join(BASE_DIR,"community/templates/community")
WALLET_DIR=os.path.join(BASE_DIR,"wallet/templates/wallet")
WALLET_STATIC=os.path.join(BASE_DIR,"wallet/static")

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

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

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# CSRF_COOKIE_SECURE=True
# SESSION_COOKIE_SECURE=True
# SECURE_HSTS_SECONDS=30
# SECURE_CONTENT_TYPE_NOSNIFF=True
# SECURE_BROWSER_XSS_FILTER=True
# X_FRAME_OPTIONS='DENY'
ALLOWED_HOSTS = ["get-onyx.herokuapp.com",'localhost']

# CELERY STUFF
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Nairobi'



# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home_page',
'dashboard',
'community',
'wallet',
'actstream',
'django_celery_results'
]

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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]

ACTSTREAM_SETTINGS ={
'MANAGER': 'actstream.managers.ActionManager',
'FETCH_RELATIONS': True,
'USE_PREFETCH': True,
'USE_JSONFIELD': False,
'GFK_FETCH_DEPTH': 1,
}
ACTSTREAM_ACTION_MODELS=['auth.User']
ROOT_URLCONF = 'Tanta.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_dir, COMMUNITY_DIR, WALLET_DIR,],
'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',
],
},
},
]

WSGI_APPLICATION = 'Tanta.wsgi.application'


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

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.Argon2PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
]

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS':{'min_length':6}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/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/1.10/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS=[

STATIC_DIR, WALLET_STATIC,
]
MEDIA_ROOT=MEDIA_DIR
MEDIA_URL='/media/'
LOGIN_URL='home_page/sign_in/'

这是我的index.html将我带到错误页面的链接:

<li><a class= 'navbar-link' href="{% url 'wallet:wallet_summary' %}">Wallet</a></li>

以下是 urls.py 中的模式调用 View :

urlpatterns=[
url(r'^$',views.wallet_summary,name="wallet_summary"),
url(r'^transfer/$',views.transfer,name='transfer'),
url(r'^info/$',views.info,name='info'),
url(r'^activity/$',views.RecentActivityView.as_view(),name='activity')

]

views.py应该呈现页面的代码片段:

def wallet_summary(request):
return render(request,'wallet/wallet_summary.html',)

访问模板的目录树如下所示:

Tanta (BASE_DIR)
|
|
wallet
|
|--------->templates
|---------------------->wallet
|----------------------------------->wallet_summary.html

index.html页面呈现得非常好,但是当我单击导航栏链接转到 wallet_summary.html 时我收到 500 错误。

最佳答案

我将其添加到我的 settings.py 文件中以将错误跟踪到控制台:

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},

'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'console':{
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.request': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': True,
},
}
}

这告诉我,我缺少一个 css 文件,导致了错误。我运行了 python manage.pycollectstatic 并确保所有静态文件都位于我告诉 Django 的位置。这使得模板能够被渲染并且现在可以工作了!

关于python - DEBUG=FALSE 时 Django 500 错误,但仅在某些页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43403717/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com