如何使用 django 发送非英文单词(中文)电子邮件

发布于 2024-10-07 20:16:43 字数 2056 浏览 3 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-10-14 20:16:43

你如何发送主题。您应该在发送之前将其编码为 utf-8。

subject.encode('utf-8')

import codecs
subject = codecs.utf_8_encode(subject)

然后将其发送到您的视图。

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.

用心笑 2024-10-14 20:16:43

现在可以了:

我使用 程序员记事本 2编码 pyhtml 文件,其中包含中文单词。

替代文字

it is ok now :

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

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文