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

django error handling
django bad request
code 400, message bad request syntax django
django raise 404
django raise 400

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()

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

Built-in Views | Django documentation, Django comes with a few views by default for handling HTTP errors. To override these with your own custom views, see Customizing error views. The 404 view is also called if Django doesn't find a match after checking every regular Django will consider the current request a 'bad request' instead of a server error. If you're uploading a file to the website when you see the error, chances are the 400 Bad Request error is due to the file being too large, and so the server rejects it. If the site permits it, compress the file to a ZIP file and then upload that instead.

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

Writing views | Django documentation, So, to recap, this view function returns an HTML page that includes the current date and time. from django.http import HttpResponse def my_view(request): # . handler400 = 'mysite.views.my_custom_bad_request_view' To test the response of a custom error handler, raise the appropriate exception in a test view​. Recently, while deploying a Django application with gunicorn and nginx I encountered the following problem: application returns Bad Request 400 for all requests. The solution was simple - just add this to your settings.py file. A list of strings representing the host/domain names that this Django site can serve.

please use SuspiciousOperation to raise bad request 400 error

from django.core.exceptions import SuspiciousOperation

def custom(): 

   raise SuspiciousOperation('bad request')

Exceptions, Format suffixes · Returning URLs · Exceptions · Status codes · Testing · Settings Exceptions… allow error handling to be organized cleanly in a central or HTTP/1.1 400 Bad Request Content-Type: application/json Content-Length: 94 You can implement custom exception handling by creating a handler function that  400 Bad Request Unfriendly huh? It's a common django problem though (as you'll see from a quick google search , and the answer is that you need to set the ALLOWED_HOSTS setting in your settings.py (or switch DEBUG back to True)

400 Bad Request Error: What It Is and How to Fix It, The 400 Bad Request Error is an HTTP response status code that servers look for custom HTTP headers to process requests and verify the client you out automatically the next time you try to load the page, so feel free to  HTTP/1.1 400 Bad Request Content-Type: application/json Content-Length: 94 {"amount": ["A valid integer is required."], "description": ["This field may not be blank."]} Custom exception handling You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects.

Handling Custom Error Pages(404, 500) In Django, 404 Page not found and 500 Internal server errors generally occur on return render(request, '404.html', status=404)def handler500(request): to see your own custom error pages served by Django's views with better UI. Update 1: Please refer this updated article for Django 2.0 and source code.. It happens very frequently that a visitor on your website typed a wrong URL or the page user is looking for no longer exists.

Django view method responses: View method , 500 (Internal Server Error), 400 (Bad Request) and 403 (Forbidden), Django Override built-in Django HTTP Status view methods in urls.py, Custom views to (like Django template tags) Jinja built-in filters & tests (like Django filters) Custom The next section on HTTP handling for the render() method describes how to  Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Comments
  • i asked about 400 not 404
  • when i logout and try to create a 404 error i dont get my custom error page which i created for 404 error instead i get a error message. " A server error occurred. Please contact the administrator.