如何将Direct Messaging链接到收件人?

时间:2016-05-31 00:27:14

标签: message sendmessage django-messages

我是一个完整的Python / Django初学者,试图向收件人撰写直接邮件。

我目前每个用户个人资料都有一个页面,其中包含指向“发送​​消息”的链接。在他们的页面上。理想情况下,一旦点击此链接,它将允许您撰写到该特定配置文件的消息。我有问题让Django知道该消息将被发送到该特定的配置文件。

到目前为止,这是我的代码:

Models.py

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',]

Views.py

def pcompose(request, username):
    title = "<h1>PCompose</h1>"
    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))

forms.py

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

pcompose.html

{% extends 'base.html' %}

    <title>{% block head_title %}Compose{% endblock %}</title>

{% block content %}

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

{% endblock %}

urls.py

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

当我运行我的代码时,我收到以下错误:

  

无法分配“(Profile:johndoe,False)”:“PDirectMessage.receiver”必须是“用户”实例。

有人会指出我的代码有任何明显的问题,这些问题不允许我向目标人员撰写邮件吗?

0 个答案:

没有答案