def create(self, validated_data): with transaction.atomic(): instance = UserInfo() instance.role_id = validated_data.get("role_id", '') instance.username = validated_data.get("username", '') password = validated_data.get("password") instance.password = make_password(password) instance.save() return instance
def register(request): username = request.POST.get('username') password = request.POST.get('password') users = UserInfo.objects.all() for user in users: if username == user.username: return JsonResponse({'res': 0}) newuser = UserInfo() newuser.username = username newuser.password = password newuser.save() return JsonResponse({'res': 1})
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): email = request.POST.get('email', '') if UserInfo.objects.filter(email=email): return render(request, 'register.html', { 'register_form': register_form, 'message': '用户已经存在!' }) pass_word = request.POST.get('password', '') user_profile = UserInfo() user_profile.username = email user_profile.email = email user_profile.is_active = False user_profile.password = make_password(pass_word) user_profile.save() # 发送邮箱,这里会生成一个16位随机数并存储到EmailVerify中 send_register_email(email, 'register') return HttpResponseRedirect(reverse('user:login')) else: return render(request, 'register.html', {'register_form': register_form})