0

I've made custom 500 and 404 error pages in Wagtail. I can preview the 404 page by typing in a false url. I'm just wondering how I can preview the 500 page?

The custom page has links to static images that I need to check are working.

My urls.py

from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin

from wagtail.contrib.sitemaps.views import sitemap
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls


admin.autodiscover()

urlpatterns = [
    url(r'django-admin/', admin.site.urls),

    url(r'^admin/', include(wagtailadmin_urls)),
    url(r'^documents/', include(wagtaildocs_urls)),

    url(r'^sitemap\.xml$', sitemap),

    url(r'', include('puput.urls')),
    url(r'', include(wagtail_urls)),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if 'debug_toolbar' in settings.INSTALLED_APPS:
    import debug_toolbar

    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]
Toms Code
  • 1,439
  • 3
  • 15
  • 34
  • Does this answer your question? [How can I trigger a 500 error in Django?](https://stackoverflow.com/questions/24660406/how-can-i-trigger-a-500-error-in-django) – nimasmi Dec 10 '19 at 09:22
  • Its the closest question/ answer on here but Wagtail doesn't have a views.py file to add a 'my_test_500_view(request)' so I'm not sure how to translate this method to wagtail – Toms Code Dec 10 '19 at 09:26
  • Okay, I've given a code example for how to integrate views. Wagtail is happy to sit alongside views modules from other apps. – nimasmi Dec 10 '19 at 09:29

2 Answers2

3

The answer at https://stackoverflow.com/a/24660486/823020 has most of these details. You can make a view that raises a 500 error.

You can add a views.py to any app. In that file (taken directly from the linked answer):

from django.http import HttpResponseServerError
def my_test_500_view(request):
        # Return an "Internal Server Error" 500 response code.
        return HttpResponseServerError()

Supplement this in your urls.py with:

from django.conf import settings
from django.urls import path
# or for Django 1.x do
# from django.urls import url
from myapp import views

urlpatterns = [
    # original content here
]

if settings.DEBUG:
    urlpatterns += [
        path('test_500/', views.my_test_500_view, name="test_500"),
        # or for Django 1.x do
        # url(r'^test_500/$', views.my_test_500_view, name="test_500"),
    ]

If it's not directly related to any Wagtail pages, then a utils Django app can work well for generic shared code.

nimasmi
  • 3,978
  • 1
  • 25
  • 41
  • Hey nimasmi, the line 'from django.urls import path' results in an error – Toms Code Dec 10 '19 at 09:54
  • 1
    Oh, apologies. `path()` was new in Django 2.0. See https://docs.djangoproject.com/en/2.2/ref/urls/#path. Are you using a 1.X version? I can update the code sample if so. – nimasmi Dec 10 '19 at 10:08
  • My bad, I should of said that I was using django 1.11 - maybe you could add the 1.11 version of the answer to the footnote/ alternative option within the answer – Toms Code Dec 10 '19 at 11:07
  • 1
    Okay, I've added Django 1.x versions as comments. You will need to remove the Django 2 `path()` lines above each of them. – nimasmi Dec 10 '19 at 11:21
  • Sorry, I must be missing something, I'm getting a 404 error page when I try and visit localhost:8000/test_500 – Toms Code Dec 10 '19 at 13:31
  • 1
    Ordinarily Django has middleware to redirect to localhost:8000/test_500/, but just in case, try with the trailing slash (or set `settings.APPEND_SLASH = True`). – nimasmi Dec 10 '19 at 13:41
  • I still get 404 after adding the slash. Where do I put settings.APPEND_SLASH = True? In the views.py? Thank you for taking the time to help me too – Toms Code Dec 10 '19 at 13:52
  • 1
    In your Django settings file, but since the slash made no difference, that might be a red herring. Are you running with `DEBUG` set to `True`? If not, maybe try moving the `test_500` URL definition outside of the `if` block, e.g. to the first entry in your `urlpatterns` list (but don't leave it there permanently). At this point you either need to post more code in your question (your full urls.py perhaps) or iterate yourself to find the solution that's particular to your project. – nimasmi Dec 10 '19 at 16:12
  • I posted my urls.py without the additions above. If more helpful, I can post with the additional code you suggested? Debug is set to True. I tried moving the test_500 inside the url patterns but no luck – Toms Code Dec 10 '19 at 16:58
  • Ah. The `include(wagtail_urls)` entry is a 'catch all' and should be the last item in your urlconf. It will match your request for `test_500/`, fail to find a Wagtail page, and return a 404 status code. Insert any other app URLs above this line. – nimasmi Dec 11 '19 at 15:33
1

Just add url with TemplateView to your urls.py:

from django.views.generic import TemplateView


urlpatterns = [
    url(r"^admin/", include(wagtailadmin_urls)),
    url(r"^documents/", include(wagtaildocs_urls)),
    url(r"^500/", TemplateView.as_view(template_name='500.html')),
]
jozo
  • 4,232
  • 1
  • 27
  • 29