From 0e9a61759bfbc6c916dbbfce412c7fe72b261a2b Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Wed, 30 Sep 2009 09:10:46 +0100 Subject: [PATCH] 404 on broken albums/photos --- sommitrealweird/photo/views.py | 50 +++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/sommitrealweird/photo/views.py b/sommitrealweird/photo/views.py index 920b1da..02d80e6 100644 --- a/sommitrealweird/photo/views.py +++ b/sommitrealweird/photo/views.py @@ -3,28 +3,34 @@ from django.http import Http404 from models import Album, Photo def album_view(request, slug): - album = Album.objects.get(slug__exact=slug) - photos = Photo.objects.filter(album=album).order_by('order', 'image') - return django.views.generic.list_detail.object_list(request, photos, paginate_by=20, template_name='photo/photo_index.html') + try: + album = Album.objects.get(slug__exact=slug) + photos = Photo.objects.filter(album=album).order_by('order', 'image') + except: + raise Http404 + return django.views.generic.list_detail.object_list(request, photos, paginate_by=20, template_name='photo/photo_index.html', allow_empty=False) def photo_view(request, slug, id): - id = int(id) - album = Album.objects.get(slug__exact=slug) - photos = Photo.objects.filter(album=album).order_by('order', 'image') - photo_qs = Photo.objects.filter(id=id) - found_prev = False - prev_photo = None - found_next = False - next_photo = None - for photo in photos: - if not found_prev: - if not photo.id == id: - prev_photo = photo - else: - found_prev = True - if found_next: - next_photo = photo - break - if photo.id == id: - found_next = True + try: + id = int(id) + album = Album.objects.get(slug__exact=slug) + photos = Photo.objects.filter(album=album).order_by('order', 'image') + photo_qs = Photo.objects.filter(id=id) + found_prev = False + prev_photo = None + found_next = False + next_photo = None + for photo in photos: + if not found_prev: + if not photo.id == id: + prev_photo = photo + else: + found_prev = True + if found_next: + next_photo = photo + break + if photo.id == id: + found_next = True + except: + raise Http404 return django.views.generic.list_detail.object_detail(request, photo_qs, object_id=id, template_name='photo/photo.html', extra_context={"next_photo": next_photo, "prev_photo": prev_photo }) -- 2.39.5