views:

63

answers:

1

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!

A: 

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.

Ofri Raviv
Brilliant! this makes sense! I'll try it right now. thanks so much!
dana
i have a problem with the view, with the two parameters:it gives me the error:profile_view() takes exactly 2 arguments (1 given)but if i pass only the id as an arg, the result is:int() argument must be a string or a number, not 'WSGIRequest'am i missing something?thanks
dana
also, i don't want the id to be the pk, i prefear the username (it is also unique), but i don't seem to succeed, the same arrors are showing up.
dana
How are you launching the view? you are accessing some url like /profile/1/ ?
Ofri Raviv
i'm launching the view by accesing http://127.0.0.1:8000/accounts/profile_view/the result is:profile_view() takes exactly 2 arguments (1 given)the view is:def profile_view(request, id): u = UserProfile.objects.get(pk=id) return render_to_response('profile/publicprofile.html') for accesing a user profile i'm accesing something like:http://127.0.0.1:8000/accounts/profile/1/and the result:DoesNotExist at /accounts/profile/1/UserProfile matching query does not existthank you
dana
/accounts/profile_view doesn't contain an id, so the profile_view method doesn't get a parameter it is expecting (you can add a default value for the id parameter if you want to avoid that). /accounts/profile_view/1/ is trying to show the user who's profile's id is 1. of course you need this to be a valid number.
Ofri Raviv
oh, yes, now i see, you're perfectly right.problem solved, thanks!
dana