1

There is some strange problem. When I do like this

def error_page(request):
    return HttpResponseBadRequest

it raises My custom error page that I listed in main URLconf. But when I pass and exception there - I got just a blank page with the text of exception. For example, view is something like this:

def error_page(request):
    return HttpResponseBadRequest('The error text')

And I get a blank page with the "The error text" text in body, but not my custom error page. How can I make Django use my Error page Template, but with error text?

I am using Python 3.5 and Django 1.10.4

Deathik
  • 123
  • 2
  • 6

1 Answers1

0

You can refer the following link for creating custom error page. Django, creating a custom 500/404 error page

For passing custom message you can use django messages

Refer: https://docs.djangoproject.com/en/1.10/ref/contrib/messages/

views.py

from django.contrib import messages


def foo(request):
    # Some view where you want to throw error
    messages.add_message(request, messages.ERROR, 'Something Not Wrong')
    raise Http404()

And in template you can load the message in th template:

in 400.html

{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

Dont forget to set DEBUG = False and ALLOWED_HOSTS = ['localhost'] in settings.py otherwise Django will catch the error.

Community
  • 1
  • 1
Pranav Aggarwal
  • 605
  • 4
  • 9
  • So, that is not a bug or something? Because I thought that there is any other way to solve that problem than making own views to deal with message of error page – Deathik Jan 02 '17 at 13:34