sumpen
sumpen

Reputation: 541

Invalid syntax when trying to create custom http errors

I'm trying to make custom http error pages. And as usual the django docs are really hard to understand.

I'm trying my best to follow this doc: https://docs.djangoproject.com/en/2.1/topics/http/views/#customizing-error-views

So I have set DEBUG = False

I have set ALLOWED_HOSTS = ['*']

In my views.py (not inside an app)

def server_error(request, exception):
    return render(request, 'mysite/errors/500.html')

And in my urls.py (not inside an app), after all paths.

urlpatterns = [

    path('', views.index, name='index'),

    .......etc

    handler500 = 'mysite.views.server_error'
]

When I run my server, I get instant error on handler500 in urls.py.

handler500 = 'mysite.views.server_error'
           ^
SyntaxError: invalid syntax

I also made a simple '500.html' under 'templates/errors'.

I have tried with importing handler, even though I read that I should not. I tried with removing 'mysite' in location for view etc.

I can't seem to find anything about this SyntaxError on my handler?

Upvotes: 0

Views: 84

Answers (1)

Davit Tovmasyan
Davit Tovmasyan

Reputation: 3588

Put handler500 = 'mysite.views.server_error' outside of urlpatterns at file level.

urlpatterns = [

    path('', views.index, name='index'),

    .......
]

handler500 = 'mysite.views.server_error'

Also add 500 response status in the error view

def server_error(request):
    return render(request, 'mysite/errors/500.html', status=500)

Upvotes: 3

Related Questions