2010-06-07 103 views
0

我有一个迷你社区,每个用户都可以搜索和查找其他用户的个人档案。 Userprofile是一个类模型,索引与用户模型类相比有所不同(用户标识不等于userprofile标识)。Django URL用户ID与userprofile id问题

但我无法通过在URL中输入相应的ID来查看用户配置文件。我只看到当前登录用户的配置文件

这是为什么? 我也想在我的网址中有用户名(也是用户表的主键),而不是id(一个数字)。

代码的有罪部分是:

我可以替换request.user用,使其实际显示我搜索的用户,而不是在当前登录?

def profile_view(request, id): 
     u = UserProfile.objects.get(pk=id) 
     cv = UserProfile.objects.filter(created_by = request.user) 
     blog = New.objects.filter(created_by = request.user) 

return render_to_response('profile/publicProfile.html', 
     { 
      'u':u, 
      'cv':cv, 
      'blog':blog, 
     }, 
     context_instance=RequestContext(request)) 

在文件urls.py(账目应用程序):

url(r'^profile_view/(?P<id>\d+)/$', 
    profile_view, 
    name='profile_view'), 

而且在模板:

<h3>Recent Entries:</h3> 

     {% load pagination_tags %} 
     {% autopaginate list 10 %} 
      {% paginate %} 
     {% for object in list %} 

      <li>{{ object.post }} <br /> 
      Voted: {{ vote.count }} times.<br /> 

      {% for reply in object.reply_set.all %} 
       {{ reply.reply }} <br /> 
      {% endfor %} 

      <a href=''> {{ object.created_by }}</a> <br /> 
      {{object.date}} <br /> 

      <a href = "/vote/save_vote/{{object.id}}/">Vote this</a> 
      <a href="/replies/save_reply/{{object.id}}/">Comment</a> </li> 
     {% endfor %} 
+0

你的网址是'/ accounts/profile_view/id',但你的模式是'^ profile_view/id'。将其更改为'^ accounts/profile_view /(?P \ d +)/ $' - 或者它只是一个错字? – Amarghosh 2010-06-07 10:07:11

+0

嗯..该网址在帐户应用下,因此帐户已添加到我的网址。如果我再次添加它,它会在我的url.it是两回事,无论如何, – dana 2010-06-07 10:10:19

+2

你是如何模板看起来像? – 2010-06-07 10:12:33

回答

2

更换

cv = UserProfile.objects.filter(created_by = request.user) 
blog = New.objects.filter(created_by = request.user) 

随着

#u is UserProfile.objects.get(pk=id) 
cv = UserProfile.objects.filter(created_by = u) 
blog = New.objects.filter(created_by = u)