settting配置
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.qq.com' # 如果是 163 改成 smtp.163.com
EMAIL_PORT = 465
EMAIL_HOST_USER = '' # 帐号
EMAIL_HOST_PASSWORD = '' # 密码
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
前段部分
<script type="text/javascript">
$(function () {
$('.register').click(function () {
$.ajax({
url:'/register',
type:'post',
dataType:'json',
data:new FormData($('#form1')[0]),
cache:false,
contentType:false,
processData:false,
success:function (res) {
if(res.code==200){
window.location.href='/'
}else {
alert(res.message)
}
}
})
})
})
</script>
py部分
#注册
import re
class Register(View):
def get(self,request):
return render(request,'index/register.html')
def post(self,request):
mes = {}
data = request.POST
# flag = validate_sql(data)
# if flag == False:
# mes['code'] = 10015
# mes['message'] = "输入的账号或密码中包含非法字符"
# return HttpResponse(json.dumps(mes))
username = data.get('user_name')
pwd = data.get('pwd')
cpwd = data.get('cpwd')
email = data.get('email')
allow = data.get('allow')
if not all([username,pwd,cpwd,email,allow]):
mes['code'] = 10011
mes['message'] = '不能为空'
else:
#用户名为8-20位
if re.match("\w{8,20}$",username) == None:
mes['code'] =10012
mes['message'] = '输入的账号不合法'
return HttpResponse(json.dumps(mes))
#邮箱
if re.match("\w{8,20}@(163|qq|126)\.com$",email) == None:
mes['code'] =10013
mes['message'] = '邮箱格式不正确'
return HttpResponse(json.dumps(mes))
#俩次输入密码是否匹配
if pwd != cpwd:
mes['code'] = 10014
mes['message'] = '俩次输入密码不一致'
yonghu = User(name=username,password=make_password(pwd),email=email,status=1,token='ad')
try:
yonghu.save()
request.session['index_user'] = username
#发送邮件
from myproo.settings import DEFAULT_FROM_EMAIL
send_mail('你好,欢迎注册本网站',"您好,欢迎注册本网站<a href='/authuser?token=asd'>"
,DEFAULT_FROM_EMAIL,['541110011@qq.com'],fail_silently=False)#fail_silently有错的时候报出来的属性
except:
mes['code'] = 10016
mes['message'] = '注册失败'
return HttpResponse(json.dumps(mes))
mes['code'] = 200
mes['message'] = '注册成功'
return HttpResponse(json.dumps(mes))