How to customize 404 page in django? - python

In my django app I want to show a custom not found 404 page. But after reading the documentation and coding I am still getting the default 404 Not found page.
I am working with Django 2.2.8
I also set DEBUG=False
Here is my code:
in setting.py I have defined:
handler404 = 'riesgo.views.views.error_404'
Definition in riesgo/views/views.py
def error_404(request, exception):
return render(request,'404.html')

In your main urls.py file
handler404 = 'mysite.views.my_custom_page_not_found_view'
you can read the docs for a more detailed answer.

Related

How to fix exception 404 in Django views?

I'm following this tutorial:https://www.w3schools.com/django/django_views.php
After copying all the code to create the members and admin views I get this error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in myworld.urls, Django tried these URL patterns, in this order:
members/
admin/
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
In your project urls.py you set the path to the members app to use 127.0.0.1:8000/members
In order to navigate to 127.0.0.1:8000/members using the default route, you need to change the path to look like this :
path('', include('members.urls')),

Error while trying to redirect 404 requests to home page in django

I would like to redirect all 404 errors to the index page of my django project. I am trying to do this by creating a view to handle all 404 requests and then adding the references to my urls.py.
This is what I have in my views.py. This is the classroom.py views in my views folder:
def view_404(request, exception=None):
return redirect('/')
and my urls.py:
handler404 = 'classroom.classroom.view_404'
However I get the error message
ERRORS:
?: (urls.E008) The custom handler404 view 'classroom.classroom.view_404' could not be imported.
HINT: Could not import 'classroom.classroom.view_404'. Parent module classroom.classroom does not exist.
Which I assume has more to do with how I am referencing the my views.py file.
Here is the structure of my project
multipleusers
-->django_project
---->templates
---->views
classroom.py
students.py
mentors.py
Then it should be :
<name_of_your_project>.<name_of_your_app>.views.classroom.view_404
if classroom is effectively the name of your file in which your views are.

Django Custom error page's giving error

I'm trying to make custom error page general 404 and 500.
I'm not trying to raise just making a default if it happens for a user, but every place I'm trying to search and follows a tutorial and so on I always ends up in getting an internal error on both 500 and 404
I'm running django=1.11.6, and I'm running debug False because I need to see if it work of course.
How can I fix this issue and make it work?
And how can I make it so it also gives the user the error text so they can send it to me?
Views.py (In my app folder)
# Error Handlers
def handler404(request):
return render(request, 'handlers/404.html', status=404)
def handler500(request):
return render(request, 'handlers/500.html', status=500)
Urls.py (in my main config folder)
from django.conf.urls import include, url, handler404, handler500
from django.contrib import admin
handler404 = 'staff.views.handler404'
handler500 = 'staff.views.handler500'
urlpatterns = [
url(r'^', include('staff.urls')),
url(r'^admin/', admin.site.urls),
]
Firstly, you don't need custom handlers if you just want to use your own templates - just put your own 404.html and 500.html files in your root templates directory.
Secondly, rather than getting users to send you the error codes manually, you can configure Django to send you errors by email.

Django - custom 403 template

I'm trying to use my 403, 404, 500 custom templates in Django 1.5 .
404 and 500 work perfectly, but 403 still showing me the built-in Django 403 template.
I put all three templates in the root template directory in my project.
They are named : 403.html, 404.html, 500.html
I also tried using:
urls.py:
from django.utils.functional import curry
handler403 = curry(permission_denied, template_name='403.html')
and also:
urls.py:
handler403 = 'proj_name.views.my_custom_permission_denied_view'
proj_name/views.py
def my_custom_permission_denied_view(request):
return ethoos_response('403.html', None, request)
Both methods do not work. Also in 404 and 500 I use none of these methods, just the templates inside the template directory, and they are shown.
All three suppose to work the same way according to Django's documentation.
https://docs.djangoproject.com/en/1.5/topics/http/views/#the-403-http-forbidden-view
I have no idea why only 403 doesn't.
Thanks.
For regular 403 permission denied pages, creating the 403.html template should work.
However, for CSRF errors (which also return status code 403), you should create a 403_csrf.html template instead.
Creating a 403_csrf.html template works in Django 1.10+. For earlier versions, you had to change the CSRF_FAILURE_VIEW setting to the view you want to use.
See the CSRF docs for more info.
There was a discussion about why the CSRF failure view behaves differently in the Django-developers mailing list this week.
You need to use 403_csrf.html.

custom handler404 => standard error page

When I try to use my own view for error 404, I instead get a standard error page. So, what I've done by now:
# root settings.py
DEBUG = False
ALLOWED_HOSTS = [
'*'
]
# blog urls.py
handler404 = 'views.custom_404'
# blog views.py
def custom_404(request):
return HttpResponse("Hello world, I'm a custom error 404")
And, besides, to try it locally on Django test server, I run it like this:
python manage.py runserver --insecure
But what I get when I go to some page which does not exist is:
Not Found
The requested url blablabla
So, for some reason I do not see my own message Hello world, I'm a custom error 404.
404 handlers are not per app, they can only be set from the main urls.py.

Categories

Resources