views:

247

answers:

2

hello, i have a mini app where users can login, view their profile, and follow each other. 'Follow' is a relation like a regular 'friend' relationship in virtual communities, but it is not necessarily reciprocal, meaning that one can follow a user, without the need that the user to be following back that person who follows him. my problem is: if i am a logged in user, and i navigate to a profile X, and push the button follow, how can i take the current profile id ?(current profile meaning the profile that I, the logged in user, am viewing right now.)

the view:

   def follow(request):
      if request.method == 'POST':
    form = FollowForm(request.POST)
    if form.is_valid():
    new_obj = form.save(commit=False)
    new_obj.initiated_by = request.user
    u = User.objects. what here?
    new_obj.follow = u   
    new_obj.save()
    return HttpResponseRedirect('.')    
   else:
       form = FollowForm()     
   return render_to_response('followme/follow.html', {
       'form': form,
       }, 
      context_instance=RequestContext(request))  

thanks in advance!

+2  A: 

Try request.user.id. But there is a better good practice. let me see.

http://docs.djangoproject.com/en/1.2/topics/db/optimization/ is a good start and is full of good practice. In your case, use request.user.id.

dzen
yep. the idea is not to take the id of the current logged in user (in my above example i am logged in) but the id of the user whose profile i am visiting and i want to follow.Thank you very much for interest!
dana
ok, imagine the url of the profile is something like /user/<id>/, then the action to follow a certain people can be resumed to a POST on an url like /user/<id>/follow/ where id is the user.id of the people you want to follow.
dzen
yes. it seems logical, and right :) i'll try it now, and post the results. thanks ! :)
dana
nope, i see now:) the problem is that i don't have the user id in the url, so i can't take it from POST:(
dana
+2  A: 

If you add the user's profile to your form, you can pass it in with your post.

There are a number of ways to do it. You could add a hidden field to your FollowForm (pass in the profile as an instance).

You could do it more manually by inserting a hidden field such as:

<input type="hidden" name="profile_id" value="{{ profile.id }}" />

Then you could change your code above to:

u = User.objects.get(request.POST['profile_id'])

Or, perhaps you already have the profile's user id in your view?

Dustin
it seems right, but i get an error : need more than 1 value to unpackand i don't see why i have more than one value in the declaration of u.thanks!
dana