我有一个名为' Read Later'用户阅读以后发布的帖子存储在哪里。我在导航栏上有Read Later。现在,当用户以后没有读取帖子并点击“稍后阅读”时,用户将被重定向回原始页面。我有一个基于函数的def for Read Later。
views.py:
def show_readlater(request, *args):
global redirect
global redirect_lock
global return_address
if not(redirect_lock):
redirect = None
else:
redirect_lock = False
if not(request.user.is_authenticated()):
raise PermissionDenied
else:
user_instance = User.objects.get(username = request.user.username)
userprofile_instance = UserProfile.objects.get(user = user_instance)
readlater_objects = ReadLaterList.objects.all()
readlater_list = []
count = 0
for x in readlater_objects:
if x.username == request.user.username:
readlater_post = Post.objects.get(slug = x.slug)
readlater_list.append(readlater_post)
count = count + 1
if count == 0 :
redirect = "no_readlater"
redirect_lock = True
return HttpResponseRedirect(return_address) # The user is redirect back to the original page
post_title = "Read Later"
template = "posts/show_posts.html"
dictionary = {
"total_posts" : readlater_list,
"title" : post_title,
"count" : count,
"redirect" : redirect,
}
return render(request, template, dictionary)
此处,redirect
将在原始页面中显示没有“稍后阅读”帖子的消息。
问题是,当重定向发生时,Django说找不到页面,但刷新后页面被加载。
发生了什么事?
答案 0 :(得分:1)
首先改变这个
global redirect
global redirect_lock
global return_address
到
request.session['redirect']= redirect
表示所有全局变量
并使用redirect
代替HttpResponseRedirect
答案 1 :(得分:0)
确保全局变量“return_address”中指定的网址正确并在app / urls.py中指定。