def create_user(request): """ ./register POST username: 用户名 password: 密码 email: 邮箱 """ username = request.POST['username'] email = request.POST['email'] password = request.POST['password'] user = User.objects.create_user(username, email, password) profile = Profile(user=user) profile.save() return HttpResponse("创建新用户成功")
def signup(request): form = SignUpForm(request.POST) if form.is_valid(): form.cleaned_data['username'] = form.cleaned_data.get('email') user = User.objects.create(**form.cleaned_data) user.set_password(form.cleaned_data.get('password')) user.profile = Profile() user.profile.token = get_random_string(length=32) user.profile.save() user.save() dic = { 'token': user.profile.token, 'email': user.email, } return JsonResponse(dic, content_type="application/json", safe=False) else: return JsonResponse({'errors': form.errors}, content_type="application/json", status=400, safe=False)
def user_register(request): context = {} if request.method == 'GET': context['form'] = RegisterForm() return render(request, 'game/register.html', context) form = RegisterForm(request.POST) if not form.is_valid(): context['form'] = form return render(request, 'game/register.html', context) new_user = User.objects.create_user(username=form.cleaned_data['username'], password=form.cleaned_data['password'], email=form.cleaned_data['email'], first_name=form.cleaned_data['first_name'], last_name=form.cleaned_data['last_name']) new_user.save() default_profile = Profile(user=new_user) default_profile.gender = form.cleaned_data['gender'] default_profile.save() login(request, new_user) return redirect(reverse('home'))