预期字符串或缓冲区:私人消息应用程序

时间:2015-09-05 07:32:36

标签: python django

我正在尝试为django上的网站制作私信消息。

Models.py:

class Message(models.Model):
    sender = models.ForeignKey(User, related_name='sender')
    recipient = models.ForeignKey(User, related_name='recipient')
    sent_date = models.DateTimeField(blank=True, null=True)
    title = models.CharField(max_length=70, default='Без теми', blank=True, null=True)
    body = models.TextField(max_length=10000)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'повідомлення'
        verbose_name_plural = 'Повідомлення'

Views.py:

@login_required
def write(request):
    context = {}
    context.update(csrf(request))
    context['form'] = WriteMessage()
    if request.POST:
        write_form = WriteMessage(request.POST)
        if write_form.is_valid():
            cd = write_form.cleaned_data
            if User.objects.filter(username=cd['recipient']).exists():
                message = Message(sender = request.user, recipient=User.objects.get(username = cd['recipient']), title=cd['title'], body=cd['body'], sent_date=datetime.now)
                message.save()
                return redirect('/inbox/')
            else:
                context['errors'] = ["Not found user with this username"]
                return render(request, 'send_message.html', context)
    else:
        return render(request, 'send_message.html', context)

当我尝试发送消息时,我收到错误:expected string or buffer。但是,当我从管理页面发送消息时 - 它很棒。

我必须做什么?请帮帮我。感谢。

1 个答案:

答案 0 :(得分:-1)

我的解决方案是替换sent_date = models.DateTimeField(blank=True, null=True)的{​​{1}}并删除sent_date = models.DateTimeField(auto_now_add=True)以在views.py中创建新对象

看来,问题出现在sent_date=datetime.nowDateFieldmodels.py模块的不同类型的数据中......