From: Brett Parker Date: Fri, 22 May 2015 16:13:13 +0000 (+0100) Subject: Make photo albums work X-Git-Url: https://git.sommitrealweird.co.uk/sommitrealweird.git/commitdiff_plain/5f6086698809e16bfe5c6c5316d1ed687cf56dc9?ds=inline Make photo albums work --- diff --git a/sommitrealweird/photo/templates/photo/photo.html b/sommitrealweird/photo/templates/photo/photo_detail.html similarity index 100% rename from sommitrealweird/photo/templates/photo/photo.html rename to sommitrealweird/photo/templates/photo/photo_detail.html diff --git a/sommitrealweird/photo/templates/photo/photo_index.html b/sommitrealweird/photo/templates/photo/photo_list.html similarity index 93% rename from sommitrealweird/photo/templates/photo/photo_index.html rename to sommitrealweird/photo/templates/photo/photo_list.html index 27e4417..5581243 100644 --- a/sommitrealweird/photo/templates/photo/photo_index.html +++ b/sommitrealweird/photo/templates/photo/photo_list.html @@ -28,8 +28,8 @@ {% else %} | {% endif %} - {% ifequal paginator_page page %} - {{ page }} + {% ifequal paginator_page page_obj.number %} + {{ paginator_page }} {% else %} {{ paginator_page }} {% endifequal %} diff --git a/sommitrealweird/photo/urls.py b/sommitrealweird/photo/urls.py index dbea64f..d5666e5 100644 --- a/sommitrealweird/photo/urls.py +++ b/sommitrealweird/photo/urls.py @@ -1,9 +1,9 @@ from django.conf.urls import url from models import Album -from views import AlbumListView +from views import AlbumListView, PhotoListView, PhotoView urlpatterns = [ url(r'^$', AlbumListView.as_view()), - url(r'^(?P[^/]*)/$', 'photo.views.album_view'), - url(r'^(?P[^/]*)/(?P[0-9]+)/$', 'photo.views.photo_view'), + url(r'^(?P[^/]*)/$', PhotoListView.as_view()), + url(r'^(?P[^/]*)/(?P[0-9]+)/$', PhotoView.as_view()), ] diff --git a/sommitrealweird/photo/views.py b/sommitrealweird/photo/views.py index 77b4dd8..28955ab 100644 --- a/sommitrealweird/photo/views.py +++ b/sommitrealweird/photo/views.py @@ -1,4 +1,4 @@ -from django.views.generic import ListView +from django.views.generic import ListView, DetailView from django.http import Http404 from models import Album, Photo @@ -11,35 +11,45 @@ class AlbumListView(ListView): context = super(AlbumListView, self).get_context_data(**kwargs) return context -def album_view(request, slug): - 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): - try: - id = int(id) - album = Album.objects.get(slug__exact=slug) +class PhotoListView(ListView): + model = Photo + paginate_by = 20 + order_by = 'order, image' + + def get_queryset(self): + album = Album.objects.get(slug__exact=self.kwargs['slug']) + return Photo.objects.filter(album=album) + +class PhotoView(DetailView): + model = Photo + + def get_object(self, **kwargs): + return Photo.objects.get(id=self.kwargs['id']) + + def get_context_data(self, **kwargs): + context = super(PhotoView, self).get_context_data(**kwargs) + album = Album.objects.get(slug__exact=self.kwargs['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: + if not photo.id == int(self.kwargs['id']): prev_photo = photo else: found_prev = True if found_next: next_photo = photo break - if photo.id == id: + if photo.id == int(self.kwargs['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 }) + + context['next_photo'] = next_photo + context['prev_photo'] = prev_photo + + return context +