简体   繁体   English

URL页面重定向Django

[英]URL Page redirect Django

I am making an application similar to twitter. 我正在制作类似于twitter的应用程序。 On each persons profile page there is an option for the current logged in user to follow the person who's profile page they are viewing. 在每个人的个人资料页面上,当前登录用户都有一个选项可以关注他们正在查看的个人资料页面。 I have a follow/unfollow button and it works, however, once clicked it redirects to an error page, but if you go back and refresh you can see that if you were following them you are now unfollowing them. 我有一个关注/取消关注按钮,它可以工作,但是,一旦单击它,它就会重定向到错误页面,但是如果您返回并刷新,可以看到如果您正在关注它们,那么您现在就取消关注它们。 So my question is once the button is clicked how do I get it to redirect to the same page? 所以我的问题是,一旦单击按钮,如何将其重定向到同一页面?

Here is the error I get: 这是我得到的错误:

_reverse_with_prefix() argument after * must be a sequence, not Profile ... *之后的_reverse_with_prefix()参数必须是序列,而不是Profile ...

/home/skybluep/thesite/panda/twitter/views.py in follow /home/skybluep/thesite/panda/twitter/views.py跟随

65 return HttpResponseRedirect(reverse('twitterindex:detail', args = profile)) 65 return HttpResponseRedirect(reverse('twitterindex:detail',args = profile))

My Views: 我的意见:

@login_required
def follow(request, username): 
    profile = get_object_or_404(Profile, user__username=username)
    user_profile = get_object_or_404(Profile, user=request.user)
    user_profile.following.add(profile)
    user_profile.save()
    return HttpResponseRedirect(reverse('twitterindex:detail', args = profile))

@login_required
def unfollow(request, username):
    profile = get_object_or_404(Profile, user__username=username)
    user_profile = get_object_or_404(Profile, user=request.user)
    user_profile.following.remove(profile)
    user_profile.save()
    return HttpResponseRedirect(reverse('twitterindex:detail', args = profile))

My App URLS 我的应用程式网址

urlpatterns = patterns('',
url(r'^$', views.index, name='twitterindex'),
url(r'^detail/(?P<username>\w+)/$', views.detail, name='detail'),
url(r'^detail/(?P<username>\w+)/newpost/$', views.post, name='newpost'),
url(r'^detail/(?P<username>\w+)/follow', views.follow, name='follow'),
url(r'^detail/(?P<username>\w+)/unfollow', views.unfollow, name='unfollow'),

)

该错误很明显,您应该执行以下操作:

return HttpResponseRedirect(reverse('twitterindex:detail', args=(profile.user.username,) ))

try: return HttpResponseRedirect(reverse('twitterindex:detail', args = profile.user.username)) . 尝试: return HttpResponseRedirect(reverse('twitterindex:detail', args = profile.user.username)) also you don't need user_profile.save() 你也不需要user_profile.save()

Better method: 更好的方法:

@login_required
def follow(request, username): 
    profile = get_object_or_404(Profile, user__username=username)
    user_profile = get_object_or_404(Profile, user=request.user)
    user_profile.following.add(profile)        
    return HttpResponseRedirect(reverse('twitterindex:detail', args = profile.user.username))

@login_required
def unfollow(request, username):
    profile = get_object_or_404(Profile, user__username=username)
    user_profile = get_object_or_404(Profile, user=request.user)
    user_profile.following.remove(profile)    
    return HttpResponseRedirect(reverse('twitterindex:detail', args = profile.user.username))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM