简体   繁体   English

django链接到社区中的任何用户个人资料

[英]django link to any user profile in social comunity

i am trying to build a virtual comunity, and i have a profile page, and a personal page. 我正在尝试建立一个虚拟社区,并且我有一个个人资料页面和一个个人页面。 In the profile page, one can see only the posts of one user(the user whos profile is checked), in the personal page one can see his posts, plus all the posts he has subscribed to (just like in Facebook) 在个人资料页面中,只能看到一个用户的帖子(选中其用户的用户),在个人页面中,可以看到他的帖子以及他已订阅的所有帖子(就像在Facebook中一样)

it's a little confusing for me how i can link to the profile of one user, i mean when anybody clicks on a username, it should link to his personal profile page. 我如何才能链接到一个用户的个人资料,这让我有些困惑,我的意思是当任何人单击用户名时,它都应链接到他的个人资料页面。 for example, if someone searches name "abc", the rsult would be "abc",and link to his profile. 例如,如果某人搜索名称“ abc”,则结果将是“ abc”,并链接到他的个人资料。

How can i pass to one function the username or id of a linked user? 如何将链接用户的用户名或ID传递给一个函数? i mean, showing the profile of the logged in user who is checking his profile is quite easy.But how about another user profile, if one wants to access it? 我的意思是,显示正在检查其个人资料的已登录用户的个人资料非常容易。但是,如果要访问另一个用户个人资料呢?

thanks a lot! 非常感谢!

You need to have a url pattern: 您需要一个网址格式:

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

a view: 一个看法:

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

In every template where you want to link to someone's profile you can use: 在要链接到某人的个人资料的每个模板中,可以使用:

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

Assuming the template knows the user you want to link to as u . 假设模板知道您要链接为u

You can also adapt this idea to use slugs instead of IDs. 您还可以使这种想法适应使用段塞而不是ID。

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

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