码迷,mamicode.com
首页 > 其他好文 > 详细

30、Django实战第30天:修改邮箱和用户信息

时间:2018-03-27 16:39:58      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:nbsp   elf   setting   sendemail   邮箱   har   技术分享   set   连接   

修改个人邮箱需要完成两个接口,一个是获取验证码,一个是新的邮箱和验证码是否匹配

1、编辑users.views.py

class SendEmailCodeView(LoginRequiredMixin, View):
    def get(self, request):
        email = request.GET.get(‘email‘, ‘‘)
        if UserProfile.objects.filter(email=email):
            return HttpResponse(‘{"email":"该邮箱已被注册"}‘, content_type=‘application/json‘)
        sendEmail(email, ‘hmail‘)
        return HttpResponse(‘{"status":"success"}‘, content_type=‘application/json‘)

2、编辑users.urls.py

from .views import SendEmailCodeView

urlpatterns = [
    ...
    url(r‘sendemail_code/$‘, SendEmailCodeView.as_view(), name=‘sendemail_code‘),
]

3、编辑utils.email_send.py

from random import Random
from django.core.mail import send_mail
from users.models import  EmailVerifyRecord
from mxonline.settings import EMAIL_FROM


def random_str(randomlenght=8):
    str = ‘‘
    chars = ‘AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789‘
    length = len(chars) - 1
    random = Random()
    for i in range(randomlenght):
        str += chars[random.randint(0, length)]
    return str


def sendEmail(email, send_type=‘register‘):
    email_record = EmailVerifyRecord()
    if send_type == ‘hmail‘:
        code = random_str(4)
    else:
        code = random_str(16)
    email_record.code = code
    email_record.email = email
    email_record.send_type = send_type
    email_record.save()

    if send_type == ‘register‘:
        email_title = ‘慕学在线网激活链接‘
        email_body = ‘请点击下面的链接激活你的账号:http://127.0.0.1:8000/active/{0}‘.format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

    elif send_type == ‘forget‘:
        email_title = ‘慕学在线网重置密码连接‘
        email_body = ‘请点击下面的链接重置你的密码:http://127.0.0.1:8000/reset/{0}‘.format(code)

        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

    elif send_type == ‘hmail‘:
        email_title = ‘慕学在线网修改邮箱‘
        email_body = ‘您的邮箱验证为:{0}‘.format(code)
        send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
        if send_status:
            pass

4、编辑usercenter-base.html

技术分享图片

5、编辑deco-user.js

技术分享图片

 

30、Django实战第30天:修改邮箱和用户信息

标签:nbsp   elf   setting   sendemail   邮箱   har   技术分享   set   连接   

原文地址:https://www.cnblogs.com/sellsa/p/8657695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!