def post(self): parse = parser_post.parse_args() user = User() user.name = parse.get('name') # user.password = parse.get('password') # 密码加密处理 user.password = generate_password_hash(parse.get('password')) user.email = parse.get('email') user.icon = parse.get('icon') user.token = str(uuid.uuid4()) returndata = {} users = User.query.filter(User.name == user.name).filter(User.email == user.email) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '用户名和邮箱已存在,请直接登录!' return returndata else: users = User.query.filter(User.email == user.email) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '邮箱已存在,请重新输入' return returndata users = User.query.filter(User.name == user.name) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '用户名已存在,请重新输入!' return returndata db.session.add(user) db.session.commit() # send_mail(user) returndata['status'] = 200 returndata['msg'] = '注册成功' returndata['data'] = user return returndata
def post(self, request): # 获取用户输入数据 username = request.POST.get('username') password = request.POST.get('password') email = request.POST.get('email') # 判断用户名是否存在 try: user = User.objects.get(name=username) except User.DoesNotExist: user = None if user: return render(request, 'myApp/register.html', {'errmsg': "用户名重复"}) # 判断邮箱是否被注册 try: user = User.objects.get(email=email) except User.DoesNotExist: user = None if user: return render(request, 'myApp/register.html', {'errmsg': "该邮箱已被注册"}) # 判断数据是否完整 if not all([username, password, email]): return render(request, 'myApp/register.html', {'errmsg': "数据不完整"}) # 判断邮箱格式是否正确 if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'myApp/register.html', {'errmsg': "邮箱格式不正确"}) # 用户注册信息存入数据库 auth_user = UserRe.objects.create_user(username, email, password) user = User() user.name = auth_user.username user.email = auth_user.email user.password = password user.save() # 向用户发送邮件 secret = Secret(settings.SECRET_KEY, 3000) info = {'id': user.id} token = secret.dumps(info) token = token.decode() subject = '美食网站欢迎信息' message = '' sender = settings.EMAIL_FROM receiver = [email] html_message = '<h1>欢迎您:%s</h1>请点击下面链接激活您的账户<br/>' \ '<a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>' % ( username, token, token) send_mail(subject, message, sender, receiver, html_message=html_message) return redirect(reverse('myApp:index'))