修改个人邮箱需要完成两个接口,一个是获取验证码,一个是新的邮箱和验证码是否匹配
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
