django编码UnicodeEncodeError

时间:2017-03-14 08:08:42

标签: python django

我在django项目中遇到了问题。

/ contact / 1 / view' ascii'中的UnicodeEncodeError编解码器不能解码位置28中的字节0xd0:序数不在范围内(128)

Traceback中的

:single_contact中的views.py

'vcard_str': unicode(VCard(contact)),

和所有观点

def single_contact(request, pk):
    contact = Contact.objects.get(pk = pk)
    if contact.group.user != request.user.profile:
        raise Http404
    if request.method=="GET":
        emails = Email.objects.filter(contact = contact)
        hash = ''
        if emails:
            email = emails[0]
            hash = get_hash(email.email)
        addresses = Address.objects.filter(contact = contact)
        if addresses:
            address = addresses[0]
        phones = PhoneNumber.objects.filter(contact=contact)
        return render(request,'dashboard/addressbook/single_contact.html',
            RequestContext(request, {
                'contact':contact, 'emails':emails, 'hash':hash,
                'addresses':addresses, 'phones':phones,
                'vcard_str': unicode(VCard(contact)),
            }))
    elif request.method=="POST":
        contact.delete()
        return HttpResponseRedirect(reverse('addressbook_index'))
    else:
        raise Http404

但如果我改变刺痛

'vcard_str': unicode(VCard(contact)),

'vcard_str':  VCard(contact).encode("utf-8")

我收到了另一个错误: / contact / 1 / view

中的AttributeError
VCard object has no attribute encode

我做错了什么?我怎样才能解决这个问题?

0 个答案:

没有答案