0

我正在制作一个类似于 twitter 的应用程序。在每个人的个人资料页面上,当前登录的用户都有一个选项可以关注他们正在查看的个人资料页面的人。我有一个关注/取消关注按钮,它可以工作,但是,一旦单击它就会重定向到错误页面,但是如果您返回并刷新,您会看到如果您关注它们,那么您现在正在取消关注它们。所以我的问题是,一旦单击按钮,我如何让它重定向到同一页面?

这是我得到的错误:

* 之后的 _reverse_with_prefix() 参数必须是一个序列,而不是 Profile ...

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

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

我的观点:

@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))

我的应用网址

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'),

)
4

2 回答 2

2

错误很清楚,你应该这样做:

return HttpResponseRedirect(reverse('twitterindex:detail', args=(profile.user.username,) ))
于 2013-10-10T16:33:20.380 回答
0

试试:return HttpResponseRedirect(reverse('twitterindex:detail', args = profile.user.username))。你也不需要user_profile.save()

更好的方法:

@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))
于 2013-10-10T17:14:23.733 回答