开发者

django hidden field error

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!


it should be

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

because the input's name is 'owner!!


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

And show me output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜