1

This error comes by default and I want to customize it

enter image description here

I have tried the code bellow to generate custom error/404/500 page but its not working at all, actually I want to raise this error when user tries to enter wrong url,then a custom error page with 404/500 would be displayed to the user.

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)

app 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
BDL
  • 21,052
  • 22
  • 49
  • 55

1 Answers1

1

It will work when you make DEBUG = False, and do not forget to add ALLOWED_HOSTS = ['localhost', '127.0.0.1']

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '22 at 06:20