Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom 404 page not working

So, in my projects I wanted to add a custom 404 error page, so I followed what I could find on the web, but nothing seemed to work for me.

This is what I have on my files:

settings.py

import os
# Django settings for HogwartsMail project.

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

DEBUG = False
TEMPLATE_DEBUG = DEBUG

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ["*"]

urls.py

urlpatterns = patterns('',
    ...
)

handler404 = "HogwartsMail.views.error404"

views.py

def error404(request):
    return render(request,'404.html')

Whenever I try to enter a random url, though, instead of getting a 404 error, I get a Server Error (500).

As I've said, i tried it many different ways, but none actually worked for me.

Also, another problem I am having is that the pages I have load really slowly and they don't open the styling or images, so I assume that, when DEBUG = False, I need everything to be already online. Is that correct?

like image 260
AugustoQ Avatar asked Dec 14 '13 07:12

AugustoQ


People also ask

What is 404 error Django?

The 404 error is raised when the URL called upon doesn't exist or has not been defined yet. This is commonly referred to as Page does not exist error. To handle requests from the undefined URLs in a website an error page is created.

Why do we get 404 error?

404 error codes are generated when a user attempts to access a webpage that does not exist, has been moved, or has a dead or broken link. The 404 error code is one of the most frequent errors a web user encounters. Servers are required to respond to client requests, such as when a user attempts to visit a webpage.


Video Answer


1 Answers

When DEBUG = False, you can use python manage.py runserver --insecure to serve the static files during local development.

See more information in this answer.

like image 54
Shengwei Avatar answered Oct 05 '22 16:10

Shengwei