如何使用Django发送非英语单词(中文)电子邮件 [英] how to send a non-english word (chinese) email using django

查看:91
本文介绍了如何使用Django发送非英语单词(中文)电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用中文单词主题:

if i use a chinese word subject :

subject = u'邮件标题'

将会显示错误:

UnicodeDecodeError at /account/login_view/

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

我该怎么办,

谢谢

已更新

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,})

推荐答案

现在可以了:

我使用程序员的记事本2 Encoding pyhtml文件,该文件具有中文单词.

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

这篇关于如何使用Django发送非英语单词(中文)电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆