How to link Direct Messaging to recipient?
<p>I am a complete Python/Django beginner, trying to compose a direct message to a recipient.</p>

<p>I currently have a page for each user profile, with a link to "Send a message!" on their page. Ideally, once clicking on this link, it would allow you to compose a message to that particular profile. I am having issues letting Django know that the message is to be sent to that specific profile. </p>

<p>Here is my code so far:</p>

<p>Models.py</p>

<pre><code>class Profile(models.Model):
    user = models.OneToOneField(User)
    phone = models.CharField( max_length = 135, null=True, blank=True)
    email = models.CharField( max_length = 135, null=True, blank=True)
    location = models.CharField(max_length=120, null=True, blank=True)
    picture = models.ImageField(upload_to=upload_location, null=True, blank=True)

def __unicode__(self): #__str__(self):
        return self.user.username

    def get_absolute_url(self):
        url = reverse("profile", kwargs={"username": self.user.username})
        return url



class PDirectMessageManager(models.Manager):
    def get_num_unread_messages(self, user):
        return super(PDirectMessageManager, self).filter(read=False).filter(receiver=user).count()


class PDirectMessage(models.Model):
    subject = models.CharField(max_length=150)
    body = models.CharField(max_length=3000)
    sender = models.ForeignKey(User, related_name='sent_pdirect_messages', null=True, blank=True)
    receiver = models.ForeignKey(User, related_name='received_pdirect_messages', null=True, blank=True)
    sent = models.DateTimeField(auto_now_add=False, auto_now=False, null=True, blank=True)



    def __unicode__(self):
        return self.subject


    objects = PDirectMessageManager()

    class Meta:
        ordering = ['-sent',]
</code></pre>

<p>Views.py</p>

<pre><code>def pcompose(request, username):
    title = "&lt;h1&gt;PCompose&lt;/h1&gt;"
    form = PComposeForm(request.POST or None) 

    if form.is_valid():

        send_message = form.save(commit=False)
        user = get_object_or_404(User, username=username)
        send_message.sender = request.user
        send_message.receiver = Profile.objects.get_or_create(user=user)
        send_message.sent = datetime.datetime.now()
        send_message.save()
        messages.success(request, 'Message Sent!')
        return HttpResponseRedirect(reverse('sent'))

    return render_to_response('profiles/pcompose.html', locals(), context_instance=RequestContext(request))
</code></pre>

<p>forms.py</p>

<pre><code>class PComposeForm(forms.ModelForm):
    class Meta:
        model = PDirectMessage
        fields = ('receiver','subject', 'body',)
        widgets = {
            'body': Textarea(attrs={'cols': 80, 'rows': 20}),
        }
</code></pre>

<p>pcompose.html</p>

<pre><code>{% extends 'base.html' %}

    &lt;title&gt;{% block head_title %}Compose{% endblock %}&lt;/title&gt;

{% block content %}

{{ title|safe }}
&lt;form action='' method='POST'&gt; {% csrf_token %}
    {{ form.as_p }}
&lt;input class="btn-success" type="submit"&gt;
&lt;/form&gt;

{% endblock %}
</code></pre>

<p>urls.py</p>

<pre><code>url(r'^profile/(?P&lt;username&gt;[\w.@+-]+)/$', 'profiles.views.profile_view', name='profile'),
url(r'^profile/(?P&lt;username&gt;[\w.@+-]+)/pcompose/$', 'profiles.views.pcompose', name='pcompose'),
</code></pre>

<p>When I run my code, I get the following error:</p>

<blockquote>
  <p>Cannot assign "(Profile: johndoe, False)": "PDirectMessage.receiver" must be a "User" instance.</p>
</blockquote>

<p>Would anyone point out any obvious issues with my code which won't allow me to compose a message to the intended person?</p>