Django - custom 403 template - python

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.

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')),

How to customize 404 page in django?

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.

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 admin returns 403 csrf error

I am producing a django/angular project. Django being the backend administration and Angular being the frontend/public display. I have created a Django 1.11 app and loaded all files, installed dependencies, etc. Locally, the site works fine and as expected. Also, since forms will be Angular js I commented out the django.middleware.csrf.CsrfViewMiddleware in my settings.py which I thought would disable the csrf token even being needed, but apparently not.
After setting up server and installing files the admin login page appears but I get the following error when I try and login:
Forbidden (CSRF token missing or incorrect.): /admin/login/
Any ideas on why this is happening would be greatly appreciated.
You can't commented out the 'django.middleware.csrf.CsrfViewMiddleware' in your settings.py, The CSRF middleware provides easy-to-use protection against Cross Site Request Forgeries. Since you are using Augualr js instead of django forms and views, you can set the csrftoken cookie in your browser cookies. Check this for detail: https://docs.djangoproject.com/en/1.11/ref/csrf/#module-django.middleware.csrf

Get rid of default view in Satchmo / Django?

just curious what's the proper/transparent way to get plain 404 error instead of view for the pre-defined url ?
The default install of Satchmo handles /quickorder url with a cart view. I would like to completely disable handling for this url. So far I did get is the following url replacement in url.py at the top of store tree:
from django.conf.urls.defaults import *
from satchmo_store.urls import urlpatterns
from satchmo_utils.urlhelper import replace_urlpattern
replace_urlpattern(
urlpatterns,
url(r'^quickorder/$', handler404)
)
The handler404 is from django.conf.urls.defaults but it returns Django's own content instead of the shop's default 404 page like if you would ask for a non-existent page like http://www.google.com/quickorder_somenonsenseintheurl.
Any idea how to override this while avoiding modifications of default Satchmo installation ?

Categories

Resources