def post(self, request): form = UserRegistrationForm(request.POST) if form.is_valid(): user = form.save() login(request, user) messages.success(request, "Регистрация прошла успешно") return redirect('home') print(form.errors) messages.error(request, "Ошибка регистрации") return render(request, 'authentication/register.html', {'form': form})
def registration(request): """ render registration page """ if request.user.is_authenticated: return redirect(reverse('home')) if request.method == "POST": registration_form = UserRegistrationForm(request.POST) if registration_form.is_valid(): registration_form.save() user = auth.authenticate(username=request.POST['username'], password=request.POST['password1']) if user: auth.login(user=user, request=request) messages.success(request, "You have been successful") return redirect(reverse('home')) else: messages.error( request, "Sorry there has been a problem with registration") else: registration_form = UserRegistrationForm() return render(request, 'registration.html', {"registration_form": registration_form})
def registration(request): """Registration form""" if request.user.is_authenticated: return redirect(reverse('home')) if request.method == "POST": registration_form = UserRegistrationForm(request.POST) if registration_form.is_valid(): registration_form.save() # WE also authenticate the user and log them in: user = auth.authenticate(username=request.POST['username'], password=request.POST['password1']) if user: auth.login(user=user, request=request) messages.success(request, "You have successfully been registered.") return redirect(reverse('home')) else: messages.error(request, "Unable to register") else: registration_form = UserRegistrationForm() return render(request, 'registration.html', {"registration_form": registration_form})
def post(self, request): """ Handling GET method :param request: Request to View :return: HttpResponse with code 201 if user is created or HttpResponseBadRequest if request contain incorrect data """ verification_code = request.GET.get("code", "") invitation = AdviserInvitations.get_invitation(verification_code) user_registration_form = UserRegistrationForm(json.loads(request.body)) if not user_registration_form.is_valid(): return HttpResponseBadRequest("Invalid input data. Please edit and try again.") AdviserUser.create_user(user_registration_form, invitation) invitation.close_invitation() return HttpResponse(status=201)
def post(self, request): """ Handling GET method :param request: Request to View :return: HttpResponse with code 201 if user is created or HttpResponseBadRequest if request contain incorrect data """ verification_code = request.GET.get("code", "") invitation = AdviserInvitations.get_invitation(verification_code) user_registration_form = UserRegistrationForm(json.loads(request.body)) if not user_registration_form.is_valid(): return HttpResponseBadRequest( "Invalid input data. Please edit and try again.") AdviserUser.create_user(user_registration_form, invitation) invitation.close_invitation() return HttpResponse(status=201)
def test_creating_adviser_user_on_invitation_and_form(self): """ Ensure we can crate AdviserUser using AdviserInvitation and Form """ test_user = { "first_name": "Test", "last_name": "Test", "password": "******", "confirm_password": "******" } registration_form = UserRegistrationForm(test_user) adviser_user = AdviserUser.create_user( registration_form, AdviserInvitations.objects.get(id=1)) self.assertEqual(adviser_user, AdviserUser.objects.get(id=adviser_user.id))
def signup(request): if request.method == "POST": form = UserRegistrationForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.set_password(form.cleaned_data["password1"]) user.save() return redirect(reverse("home")) else: import pdb; pdb.set_trace() messages.error(request, form.errors) template = "authentication/signup.html" form = UserRegistrationForm() context = {"form": form} return render(request, template, context)
def register(request): if request.method == "POST": user_form = UserRegistrationForm(request.POST) if user_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() profile = UserProfile.objects.create(user=new_user) return render(request, 'registration/register_done.html', { 'new_user': new_user, 'profile': profile }) else: user_form = UserRegistrationForm() return render(request, 'registration/register.html', {'user_form': user_form})
def get(self, request): form = UserRegistrationForm() return render(request, 'authentication/register.html', {'form': form})