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

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

现在输入新邮箱,点击获取验证码,邮箱就能收到邮件了

现在我们来完成另外一个接口,验证验证码

编辑users.views.py

class UpdateEmailView(LoginRequiredMixin, View):
def post(self, request):
email = request.POST.get('email', '')
code = request.POST.get('code', '') existed_records = EmailVerifyRecord.objects.filter(email=email, code=code, send_type='hmail')
if existed_records:
user = request.user
user.email = email
user.username = email
user.save()
return HttpResponse('{"status":"success"}', content_type='application/json')
else:
return HttpResponse('{"email":"验证码错误"}', content_type='application/json')

编辑users.urls.py

...
from .views import UpdateEmailView urlpatterns = [
...
url(r'update_email/$', UpdateEmailView.as_view(), name='update_email'),
]

编辑 deco-user.js

现在输入验证码,就可以完成邮箱修改了

最后我们来完成用户信息的提交

我们之前定义了UserInfoView使用了get方法来显示,现在我们可以定义post方法完成用户信息的提交

编辑users.forms.py对提交的数据进行验证

class UserInfoForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['nick_name', 'gender', 'birthday', 'address', 'mobile']
# 因为头像、密码、邮箱是单独修改的,这里就不取出来

编辑users.view.py

...
from .forms import UserInfoForm class UserInfoView(LoginRequiredMixin, View):
def get(self, request):
return render(request, 'usercenter-info.html', {}) def post(self, request):
user_info_form = UserInfoForm(request.POST, instance=request.user)
if user_info_form.is_valid():
user_info_form.save()
return HttpResponse('{"status":"success"}', content_type='application/json')
else:
return HttpResponse(json.dumps(user_info_form.errors), content_type='application/json')  

编辑usercenter-info.html

编辑 deco-user.js

最新文章

  1. Android开发之Shape详细解读
  2. Android基础开发文档汇总
  3. jquery优势
  4. iOS 的UIWindow 类研究
  5. 去除hadoop启动时的警告
  6. 在Linux下怎么确定哪个网卡对应哪个接口?
  7. notepad++ 编辑器链接地址可点击
  8. O - Extended Traffic(判断负环)
  9. dpkg, APT, aptitude常用命令
  10. Directx11学习笔记【三】 第一个D3D11程序
  11. FluentDataflow - Fluent Style TPL Dataflow
  12. 使用select为描述符设置超时
  13. Lodop背景图无图片时显示放大叉号问题
  14. Java异常及错误
  15. python 数据类型 之 tuple 元组
  16. 高性能Mysql学习笔记之事务
  17. Java并发编程(七)终结线程
  18. IO流图
  19. spring 事件驱动模型简介
  20. bzoj4069【APIO2015】巴厘岛的雕塑

热门文章

  1. 【BZOJ 2241 打地鼠】
  2. 适用于实数范围的中缀表达式的 + - * / ( ) 计算(C++实现)
  3. Kafka自我学习3-Scalable
  4. SpringMVC——说说视图解析器
  5. matlab求矩阵的鞍点
  6. MySQL rpm 版本安装
  7. css之postion定位
  8. OOP第三次上机
  9. [bzoj2152]聪聪可可——点分治
  10. 【Python实例二】之前期准备:Windows下的BeautifulSoup安装