4

i want to create my own 404 page. In settings.py I have added:

DEBUG = TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['*',]

In urls.py:

handler404 = 'blog.views.handler404'

In views.py:

def handler404(request):
    return render(request, 'blog/404.html')

Also I have created that 404.html file.

When i start server i write:

python manage.py runserver --insecure

--insecure is to provide static files (otherwise it is nonsense). But if i go non existing page i get:

<h1>Not Found</h1><p>The requested URL /post/9/ was not found on this server.</p>

How do I solve this? I am using Django 1.8 dunno if this changes anything

pptt
  • 675
  • 2
  • 8
  • 16
  • You can just put your custom 500.html/404.html in your templates directory and django will use those templates for the rendering instead of the default django templates. – Pieter Hamman Jun 16 '15 at 17:10
  • but it does not work – pptt Jun 17 '15 at 18:55
  • Is the 404.html in the same directory as you base.html? Otherwise you template path is incorrect – Pieter Hamman Jun 17 '15 at 18:58
  • Yes, it is in the same dir – pptt Jun 17 '15 at 18:59
  • Try setting DEBUG=False and see if that changes anything – Pieter Hamman Jun 17 '15 at 19:06
  • i have tried it with True and False, when it is false i get smth like this:

    Not Found

    The requested URL /post/9/ was not found on this server.

    , when it is True i get standart django debug error page
    – pptt Jun 17 '15 at 19:14
  • yes I have tried that too – pptt Jun 17 '15 at 19:19
  • maybe it is somehow different on django 1.8? does anyone knows ho do i solve this nonsense ? – pptt Jun 19 '15 at 06:00
  • @pptt - the 404 template is loaded according to the normal rules, so it can depend on the order of the apps in installed_apps - if your app with the templates directory in it comes after contrib.admin, it won't be loaded. Likewise if you're using suit, grappelli or any other library that provides a custom page. – Hayden Crocker May 02 '17 at 21:14

1 Answers1

1

You shouldn't need anything in urls.py. Go to your root views.py and add your handler404 method there, and leave urls.py alone.

Ref: Django, creating a custom 500/404 error page

Also, I don't see your TEMPLATE_DIRS variable, i.e.

TEMPLATE_DIRS = (
    '/path/to/template/files/',
    '/path/to/more/template/files/' 
)

Need to make sure that your templates in ../blog/.. are getting found properly. Personally I'd add that specifically as a subdirectory.

Community
  • 1
  • 1
JustinP
  • 11
  • 1
  • hello, i have no template dirs variable. i have added new one - TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'blog/templates/blog/'),) it is the same – pptt Jun 17 '15 at 18:56