【问题标题】:Want to generate Custom 404/500 error page in django app想要在 django 应用程序中生成自定义 404/500 错误页面
【发布时间】:2020-02-13 06:42:13
【问题描述】:

默认出现此错误,我想自定义它

我已尝试使用以下代码生成自定义错误/404/500 页面,但它根本不起作用, 实际上我想在用户尝试输入错误的 url 时引发此错误,然后将向用户显示一个带有 404/500 的自定义错误页面。

views.py

  from django.shortcuts import render
  from django.shortcuts import render
  from django.conf import settings

  def error_404(request,exception):
  context = {}
  context = {"project name":settings.PROJECT_NAME}
  return  render(request,'error_404.html',context)

  def error_500(request):
  context = {}
  context = {"project name":settings.PROJECT_NAME}
  return  render(request,'error_500.html',context)

应用urls.py

 from django.conf import settings
 from django.conf.urls.static import static
 from django.contrib import admin
 from django.urls import path
 from django.conf.urls import handler404, handler500, include
 from .import views
 import testapp
 from testapp import views as common_views
 urlpatterns = [
 path('error_404',views.error_404,'error_404'),
 path('error_500',views.error_500,'error_500'),

]

project/urls.py

   from django.contrib import admin
   from django.urls import path
   from django.conf.urls import handler404, handler500, include
   from testapp import urls
   import testapp
   from testapp import views as common_views
   urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include(testapp.urls)),

  ]
   handler404 = common_views.error_404
   handler500 = common_views.error_500

【问题讨论】:

标签: django python-3.x django-views


【解决方案1】:

当你设置 DEBUG = False 时它会起作用,并且不要忘记添加 ALLOWED_HOSTS = ['localhost', '127.0.0.1']

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2013-02-04
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多