views:

39

answers:

2

hi, there, i'm building a message system for a virtual community, but i can't take the userprofile id

i have in views.py

def save_message(request):
   if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
           new_obj = form.save(commit=False)
           new_obj.sender = request.user
           u = UserProfile.objects.get(request.POST['userprofile_id'])
           new_obj.owner = u
           new_obj.save()
           return HttpResponseRedirect('.')    
   else:
           form = MessageForm()     
   return render_to_response('messages/messages.html', {
           'form': form,
           }, 
          context_instance=RequestContext(request))  

and the template:

{% block primary %}
<form action="." method="post">
    {{ form.as_p }}
<p><input type="hidden" value="{{ userprofile.id }}" name = "owner" /></p>
<p><input type="submit" value="Send Message!" /></p>
</form>
{% endblock %}

forms.py:

class MessageForm(ModelForm):
    class Meta:
          model = Messages
          fields = ['message']

models.py:

class Messages(models.Model):
     message = models.CharField(max_length = 300)
     read = models.BooleanField(default=False)
     owner = models.ForeignKey(UserProfile)
     sender = models.ForeignKey(User) 

I don't figure out why i get this error,since i'm just trying to get the profileId of a user, using a hiddeen field.

the error is:

Key 'UserProfile_id' not found in <QueryDict: {u'owner': [u''], u'message': [u'fdghjkl']}>

and i'm getting it after i fill out the message text field. Thanks!

+1  A: 

it should be

           u = UserProfile.objects.get(request.POST['owner'])

because the input's name is 'owner!!

lazerscience
yes. u're right. but now, i get another error: need more than 0 values to unpack
dana
you need to specifiy the attribute: u = UserProfile.objects.get(pk=request.POST['owner'])
lazerscience
it says now: invalid literal for int() with base 10: ''
dana
u = UserProfile.objects.get(pk=int(request.POST['owner'])) :)
lazerscience
hmm.. i've modified,but same error here :D
dana
The problem arises because there's an empty string in your request dict with key 'owner', so i suppose it is populated wrongly before! (i don't see where 'userprofile.id' in your form should come from, therefore {{ userprofile.id }} prints an empty string, i do not exactly now what you'Re tying to achieve, but i think you need to get that value somewhere in your view and put it in the context for your form!!
lazerscience
yes. i'll try it this way then :)
dana
Question: if i have a function view named userprofile, how can i get the id of each userprofile without passing it as a parameter to the function view? i need it in the context of userprofile, so that i can pass it in the url of my 'send a message' link.thanks!
dana
I think what you want to achieve is have a link for sending a message to a certain user? so you could put the user-id at the end of the url as a get-parameter, eg. ?user_id=x (i guess you will have to append this to the links from your userprofile for sending a message), and then get this get-variable in the called view with user_id = request.GET['user_id'].
lazerscience
ythis is what i want to achieve.Through that GET parameter, i am getting the user whos profile belongs to (and the destinatar of the message), and not the currently logged in user(the sender of the message)?thanks so much!
dana
You need to put something like "<a href='sendmessage/?user_id={{ user.pk }}'" in the template of the user profile! (dont know what your url for that is exactly, maybe it'll be '/appname/send.....', and you should have the user object somewhere in the context of this page, i assumed it's user, but if it's something else, put the right id in the link--and this has nothing to do with the currently logged in user)
lazerscience
thanks so much! :)
dana
A: 

Can you set raise(raise Exception,request.POST) before string: u = UserProfile.objects.get(request.POST['userprofile_id'])

And show me output.

Saff
hello, thanks so much for support, i've just figured out how it should be done, and i'll try to post the final code,for those who might have the same problem :)
dana