Xhark
Xhark

Reputation: 841

Error while extending on 500 error django custom view

I'm triying to define a custom 500 error for my django app by just placing my 500.html file in the templates folder.

{% extends "base.html" %}
{% load static %}
{% block body %}

<div class="container"> 
    <div class="row">
      UPS, algo fue mal.
    </div>    
</div>

{% endblock %}

Whitout the first line( extending the skeleton of the web) it works, but with it i got this meesage:

A server error occurred.  Please contact the administrator.

I'm dooing the exact same thing for the 404 errors, and I works perfectly. I'm completly lost here.

Upvotes: 1

Views: 268

Answers (1)

Daniel Hepper
Daniel Hepper

Reputation: 29967

Your base.html probably expects some context which is not passed in by the default 500 view. From the documentation:

The default 500 view passes no variables to the 500.html template and is rendered with an empty Context to lessen the chance of additional errors.

If that is the case you can either:

  1. create a separate, minimal and preferable self-contained template for the 500 view
  2. make sure your base.html works properly with an empty context or
  3. create a custom 500 view that passes in the required context

I think option 1 is the preferred option because it ensures that the 500 page can always be displayed. The downside is that any changes in your base.html, you might have to apply to your 500 template as well.

Option 3 is the least preferred because it carries the highest risk of the 500 page not rendering,e.g. if the 500 is caused because the DB is unavailable and your custom 500 view tries to use the DB.

Upvotes: 2

Related Questions