在需要传递上下文的Django视图中使用HttpResponseRedirect() [英] Using HttpResponseRedirect() in a Django view where context has to be passed
问题描述
在我的Django应用程序中,我使用 ListView
类来列出用户上传的照片。可以upvote或downvote这些照片。未经身份验证的用户可以看到照片,但不允许投票。
我的用户使用不可靠的部分使用不支持JS的旧设备。对于这样的用户,upvote或downvote不启用AJAX。相反,整个页面刷新。
我确保页面刷新仍然在他们投票的同一张照片上登陆这些用户。我通过HTML中的< a name =section {{forloop_counter}}>
标签来执行此操作。
我每页显示20张照片,即20部分。投票后,我将 photo_id
转到 def get(self,request,* args,** kwargs)
ListView类。我计算出哪个 section_num
,出现photo_id。然后我只需要执行 return HttpResponseRedirect(section_num)
。这使我处于正确的位置,例如 http://example.com/?page=1#section8
。
我的问题是:而不是使用一个 ListView类
,假设我正在编写自己的自定义视图列出照片等。在这种情况下,如何实现上述 HttpResponseRedirect(section_num)
功能?一个说明性的例子将是巨大的。感谢提前!
这是我的自定义视图:
def photos_list(request,* args,** kwargs):
pre>
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)
除了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)
注意:我读了这个SO任务n ,但无法辨别在我的情况下如何帮助。其中一部分是因为我是一个初学者,也是因为那里似乎有一个不同的问题。
解决方案p>您的问题不清楚 - 但是,您是否考虑使用
< a name =section {{object.id}}>
或< div id =photo - {{object.id}}>
并重定向到#photo.id
而不是计算?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
todef get(self, request, *args, **kwargs)
method of ListView class. I calculate whichsection_num
the photo_id appears in. Then I simply doreturn 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 aforementionedHttpResponseRedirect(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
解决方案Your questions is not clear - however, did you consider using
<a name="section{{ object.id }}">
or<div id="photo-{{ object.id }}">
and redirect to the#photo.id
instead of calculating?这篇关于在需要传递上下文的Django视图中使用HttpResponseRedirect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!