In this Django app of mine, I use a ListView
class to list user-uploaded photos. One can upvote or downvote these photos. Non-authenticated users can see the photos, but not allowed to cast votes.
An unignorable section of my users use legacy devices that can't support JS. For such users, upvote or downvote isn't AJAX-enabled. Instead, the entire page refreshes.
I ensure the page refresh still lands such users on the same photo they voted. I do this via the <a name="section{{ forloop_counter }}">
tag in HTML.
I show 20 photos per page, i.e. 20 sections. Upon voting, I pass the photo_id
to def get(self, request, *args, **kwargs)
method of ListView class. I calculate which section_num
the photo_id appears in. Then I simply do return HttpResponseRedirect(section_num)
. This lands me at the correct position, e.g. http://example.com/?page=1#section8
.
My question is: instead of using a ListView class
, assume I'm writing my own custom view to list out the photos and such. How do I implement the aforementioned HttpResponseRedirect(section_num)
functionality in this case? An illustrative example would be great. Thanks in advance!
Here's my custom view:
def photos_list(request, *args, **kwargs):
form = PhotosListForm()
ob_list = retrieve_latest_photos(request.user.id)
paginator = Paginator(ob_list, 20)
page = request.GET.get('page', '1')
try:
page = paginator.page(page)
except PageNotAnInteger:
page = paginator.page(1)
except EmptyPage:
page = paginator.page(paginator.num_pages)
context = {'object_list': ob_list, 'form':form, 'page':page,'username':request.user.username}
return render(request, 'photos_list.html', context)
Note: I read this SO question, but can't discern how it helps in my case. Part of that is because I'm a beginner, but also because the op there seems to have a different problem