how to send a non-english word (chinese) email using django

时光总嘲笑我的痴心妄想 提交于 2019-12-12 19:43:26

问题


if i use a chinese word subject :

subject = u'邮件标题'

it will be show error :

UnicodeDecodeError at /account/login_view/

'utf8' codec can't decode bytes in position 0-1: invalid data

what can i do about it ,

thanks

updated

def register_view(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            # Process the data in form.cleaned_data
            # ...
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            email = form.cleaned_data['email']
            user = User.objects.create_user(username, email, password)

            send_html_mail(subject, html_content, [email])
            if user is not None:
                user.save()
                #return HttpResponse(simplejson.dumps({'msg':'ok'}))
                return HttpResponseRedirect("/")
            else:
                return HttpResponseRedirect("/account/register_view") 
    else:
        form = SignupForm() # An unbound form

    return render_to_response('accounts/register_view.html',{'form': form,})

def login_view(request): 
    if request.method == 'POST': 
        form = LoginForm(request.POST) 
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(username=username, password=password)  
            if user is not None:  
                if user.is_active:
                   login(request, user)
                   return HttpResponseRedirect("/")
                else:
                   return HttpResponse('user is not active')
            else:
                #return HttpResponseRedirect("/account/login_submit") 
                return HttpResponse('No this username . and <a href="/">return to homepage</a>')
    else:
        form = LoginForm() # An unbound form

    return render_to_response('accounts/login_view.html',{'form': form,})

回答1:


How are you sending the subject. You should encode it to utf-8 before sending.

subject.encode('utf-8')

or

import codecs
subject = codecs.utf_8_encode(subject)

And then send it to your view.




回答2:


it is ok now :

i use Programmer’s Notepad 2 to Encoding the py and html file which has chinese word .



来源:https://stackoverflow.com/questions/4447195/how-to-send-a-non-english-word-chinese-email-using-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!