beginners
beginners

Reputation: 325

how to test django 400 bad request error for custom error page?

How to test my custom error page for error 400 bad request in django

views.py

class Custom400View(TemplateView):
   template_name = "400.html"

urls.py

 handler400 = Custom400View.as_view()

Upvotes: 3

Views: 2756

Answers (4)

VIKAS Gujjar
VIKAS Gujjar

Reputation: 1

go in setting.py

do this ALLOWED_HOSTS = ['*']

Upvotes: -2

beginners
beginners

Reputation: 325

please use SuspiciousOperation to raise bad request 400 error

from django.core.exceptions import SuspiciousOperation

def custom(): 

   raise SuspiciousOperation('bad request')

Upvotes: 0

hwhite4
hwhite4

Reputation: 715

You can setup a view that just raises a SuspiciousOperation exception like this

from django.core.exceptions import SuspiciousOperation

def bad_request_view(request):
    raise SuspiciousOperation

Then add a url for the view in your urls py

Link to the docs explanation: https://docs.djangoproject.com/en/2.1/topics/http/views/#testing-custom-error-views

Upvotes: 2

rahul.m
rahul.m

Reputation: 5854

In veiws.py

def handler400(request):
    return render(request, '400.html', status=400)

In urls.py

handler400 = myapp.views.handler400

note to keep debug false

for more details refer this

Upvotes: 2

Related Questions