简体   繁体   中英

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)

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.

How can i pass to one function the username or id of a linked user? 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 .

You can also adapt this idea to use slugs instead of IDs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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