Intercept NoReverseMatch eceptions more helpfully

This commit is contained in:
Philip Sargent 2022-06-23 19:01:57 +03:00
parent 4fa8d18621
commit 77cf3455a6

View File

@ -12,6 +12,7 @@ from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.urls import NoReverseMatch
import troggle.settings as settings
from troggle.core.views import expo
@ -235,11 +236,13 @@ def rendercave(request, cave, slug, cave_id=''):
editable = False
if not cave_id:
cave_id = slug # cave.unofficial_number
try:
context = {'cave_editable': editable, 'settings': settings, 'cave': cave, 'cavepage': True,
context = {'cave_editable': editable, 'settings': settings, 'cave': cave, 'cavepage': True,
'cave_id': cave_id, 'svxstem': str(svxstem), 'svx3d':svx3d}
r = render(request, templatefile, context) # crashes here if url not set up for 'edit_cave' in urls.py
try:
r = render(request, templatefile, context) # crashes here with NoReverseMatch if url not set up for 'edit_cave' in urls.py
return r
except NoReverseMatch:
raise
except:
message = f'Failed to render cave: {slug}'
return render(request,'errors/generic.html', {'message': message})
@ -259,9 +262,9 @@ def cavepage(request, karea, subpath):
kpath = karea + subpath
# print(f" ! cavepage:'{kpath}' kataster area:'{karea}' rest of path:'{subpath}'")
cave = Cave.objects.get(url = kpath) # ideally this will be unique
print(f" ! cavepage: url={kpath} -- {cave}")
try:
cave = Cave.objects.get(url = kpath) # ideally this will be unique
# print(f" ! cavepage: url={kpath} -- {cave}")
r = rendercave(request, cave, cave.slug())
return r
@ -273,8 +276,10 @@ def cavepage(request, karea, subpath):
caves = Cave.objects.filter(url = kpath)
# we should have a -several variant for the cave pages, not just the svxcaves:
return render(request, 'svxcaveseveral.html', {'settings': settings, "caves":caves })
except NoReverseMatch:
raise
except:
message = f'Failed to find cave: {kpath}'
message = f'Failed to render cave: {kpath} (it does exist and is unique) because of a Django URL resolution error. Check urls.py.'
return render(request,'errors/generic.html', {'message': message})
return rendercave(request, cave, cave.slug(), cave_id=cave_id)