Django 3.x - Custom Error Page is showing a 500 when it should be a 404
Figured it out. Thanks ya'll.
There was an error in the function I was using to grab the page. I wasn't checking to see if the URL was valid when looking up a particular wiki entry.
Changing entry_page in views.py to this made it work!:
def entry_page(request, entry):
if entry in util.list_entries():
name = entry # Places the title
text = util.get_entry(entry) # Grabs the entry using the util function
html = md_converter(text)
return render(request, "encyclopedia/entry.html", {
"title": name,
"entry": html
})
else:
return render(request, "encyclopedia/error_404.html" )
Glad it wasn't Django messing up, just me lol
Want to generate Custom 404/500 error page in django app
It will work when you make DEBUG = False, and do not forget to add ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Django, custom error pages redirects to 500 instead of 404
This question is three months old but for the community this is how I solved this:
The 404-page-not-found-view needs an "exception" argument:
def error_404(request, exception):
return render(request,'myapp/404.html', status = 404)
The default 404 view will pass two variables to the template:
request_path, which is the URL that resulted in the error, and
exception, which is a useful representation of the exception that
triggered the view (e.g. containing any message passed to a specific
Http404 instance).
Source: https://docs.djangoproject.com/en/2.1/ref/views/#the-404-page-not-found-view
I'm Django-noob but I suppose that without this argument the 404 trigger a 500.
One more thing: It works for me without the import : "from django.conf.urls import handler404, handler500"
Common 404 & 500 Page for My Django Project having multiple application
In your templates directory create templates custom404.html
and custom500.html
Add this to your urls.py
at the bottom if application specific or in urls.py
where you include urls to other apps
handler404 = 'custom_views.handler404'
handler500 = 'custom_views.handler500'
And define views like this in custom_views
file in root once
from django.shortcuts import render_to_response
def handler404(request, *args, **kwargs):
response = render_to_response('custom404.html', context = {})
response.status_code = 404
return response
def handler500(request, *args, **kwargs):
response = render_to_response('custom500.html', context={})
response.status_code = 500
return response
This will route your 404,500 url to the template views there your are building a template and rendering it
Using static files in custom 404/500 pages in Django
Here's how I would do it:
# urls or settings
handler500 = 'mysite.views.server_error'
# views
from django.shortcuts import render
def server_error(request):
# one of the things ‘render’ does is add ‘STATIC_URL’ to
# the context, making it available from within the template.
response = render(request, '500.html')
response.status_code = 500
return response
It's worth mentioning the reason Django doesn't do this by default:
“The default 500 view passes no variables to the 500.html template and is rendered with an empty Context to lessen the chance of additional errors.”
-- Adrian Holovaty, Django documentation
Related Topics
Logger Configuration to Log to File and Print to Stdout
Difference in Boto3 Between Resource, Client, and Session
Running Ruby, Node, Python and Docker on the New Apple Silicon Architecture
Remove Xticks in a Matplotlib Plot
How to Convert SQL Query Result to Pandas Data Structure
Sqlalchemy: Print the Actual Query
SQL Join or R's Merge() Function in Numpy
Python Code to Remove HTML Tags from a String
Pandas Read_HTML Valueerror: No Tables Found
Why Is Variable1 += Variable2 Much Faster Than Variable1 = Variable1 + Variable2
Beautifulsoup Not Grabbing Dynamic Content
Django Gunicorn Not Load Static Files
Why Doesn't This Division Work in Python
Combine a Folder of Text Files into a CSV with Each Content in a Cell
How to Overcome Typeerror: Unhashable Type: 'List'