【问题标题】:django link to any user profile in social comunitydjango 链接到社交社区中的任何用户个人资料
【发布时间】:2010-05-26 10:49:01
【问题描述】:

我正在尝试建立一个虚拟社区,我有一个个人资料页面和一个个人页面。在个人资料页面中,只能看到一个用户的帖子(查看个人资料的用户),在个人页面中,可以看到他的帖子,以及他订阅的所有帖子(就像在 Facebook 中一样)

我有点困惑如何链接到一个用户的个人资料,我的意思是当任何人点击用户名时,它应该链接到他的个人资料页面。 例如,如果有人搜索名称“abc”,结果将是“abc”,并链接到他的个人资料。

如何将链接用户的用户名或 ID 传递给一个函数? 我的意思是,显示正在检查其个人资料的登录用户的个人资料非常容易。但是如果有人想访问另一个用户个人资料呢?

非常感谢!

【问题讨论】:

  • 个人资料页面的网址是否包含用户名?

标签: django templates hyperlink profile


【解决方案1】:

你需要有一个 url 模式:

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

一个视图:

def profile_view(request, id):
    u = UserProfile.objects.get(pk=id) # this is the user who's profile is being viewed.
    # here's your view logic
    # render a user's profile template in the end

在您想要链接到某人的个人资料的每个模板中,您都可以使用:

<a href="{% url profile u.id %}">{{ u.username }}</a>

假设模板知道您要链接到的用户为u

您也可以调整这个想法以使用 slug 而不是 ID。

【讨论】:

  • 太棒了!这是有道理的!我马上试试。非常感谢!
  • 我的视图有问题,有两个参数:它给了我错误:profile_view() 正好需要 2 个参数(给定 1 个)但是如果我只将 id 作为 arg 传递,则结果是: int() 参数必须是字符串或数字,而不是 'WSGIRequest' 我错过了什么吗?谢谢
  • 另外,我不希望 id 是 pk,我更喜欢用户名(它也是唯一的),但我似乎没有成功,同样的错误出现了。
  • 如何启动视图?您正在访问一些网址,例如 /profile/1/ ?
  • 我通过访问127.0.0.1:8000/accounts/profile_view 来启动视图,结果是:profile_view() 正好需要 2 个参数(1 个给定)视图是:def profile_view(request, id): u = UserProfile. objects.get(pk=id) return render_to_response('profile/publicprofile.html') 用于访问我正在访问的用户配置文件:127.0.0.1:8000/accounts/profile/1 和结果:DoesNotExist at /accounts/profile/1/ UserProfile 匹配查询不存在谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2012-09-19
  • 1970-01-01
相关资源
最近更新 更多