def register(request): if request.method == 'POST': form = UserRegistrationForm(request.POST) if form.is_valid(): #do an early save so that if user input is invalid/ unauthorised new form can be loaded with valide details previously entered form.save() #user has a valid username and password, if not a user then we will drop out of this code and reload form on line X #else: #form= UserRegistrationForm() user = auth.authenticate(username=request.POST.get('username'), password=request.POST.get('password1')) #if valid user, then return success msg and redirect to profile. Else provide error msg. if user: auth.login(request, user) messages.success( request, "You have successfully registered and are now logged into your account" ) return redirect(reverse('profile')) else: messages.error(request, "Unable to log you in at this time.") else: form = UserRegistrationForm() args = {'form': form} args.update(csrf(request)) return render(request, 'register.html', args)
def register(request): uform = UserRegistrationForm(request.POST or None) if request.method == "POST": if uform.is_valid(): uform.save() user = authenticate(email=uform.cleaned_data.get('email'), password=uform.cleaned_data.get('password1')) if user: login(request, user) return redirect(reverse('register-profile-page')) return render(request, 'user/register.html', locals())
def user_register(request): """ View for registering new users. If user is already authenticated view redirects to index page. """ register_form = UserRegistrationForm(request.POST) if register_form.is_valid(): register_form.save() registered_user = authenticate(username=register_form.cleaned_data['username'], password=register_form.cleaned_data['password']) auth_login(request, registered_user) return redirect(settings.LOGIN_REDIRECT_URL) return render(request, 'index.html', {'register_form': register_form, 'display': 'block', 'active': 'register'})
def register(request): if request.method == "POST": form = UserRegistrationForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') email = form.cleaned_data.get('email') messages.success( request, f"Hey {username}, your account has been created successfully!") return redirect('user-login') else: form = UserRegistrationForm() context = {'form': form} return render(request, "user/register.html", context)
def register(request): if request.method == 'POST': u_form = UserRegistrationForm(request.POST) p_form = UserProfileForm(request.POST, request.FILES) if u_form.is_valid() and p_form.is_valid(): check = User.objects.filter( email=u_form.cleaned_data.get('email')).exists() if check is False: user = u_form.save() user.save() designation = p_form.cleaned_data.get('designation') if designation == 'TEACHER': group = Group.objects.get(name='TEACHERS') user.groups.add(group) else: group = Group.objects.get(name='STUDENTS') user.groups.add(group) profile = p_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() messages.success( request, "Your account has been created. Click Log In to Sign In") return redirect('index') else: messages.warning(request, 'Email already exists') return redirect('index') return render(request, 'ams/index.html')
class TestUserRegistrationForm(TestCase): def setUp(self): self.fakeUser = Faker() self.data = { 'email': self.fakeUser.email(), 'username': self.fakeUser.user_name(), 'firstname': self.fakeUser.first_name(), 'lastname': self.fakeUser.last_name(), 'password1': 'secretdeadlypassword', 'password2': 'secretdeadlypassword', 'date_of_birth': self.fakeUser.date_of_birth(), 'image': factory.django.ImageField( from_path= r"C:\Users\Elisha\Pictures\Screenshots\Screenshot (16).png", filename=r"\newimage", format="png") } self.form = UserRegistrationForm(data=self.data) self.request = HttpRequest() self.request.method = 'POST' self.request.FILES['image'] = self.data['image'] self.request.POST = self.data self.form.is_valid() engine = import_module(settings.SESSION_ENGINE) session_key = None self.request.session = engine.SessionStore(session_key) self.request.META['SERVER_NAME'] = 'testserver' self.request.META['REQUEST_METHOD'] = 'POST' self.user = self.form.save(self.request) pass def test_form_is_valid(self): self.assertTrue(self.form.is_valid()) def test_form_saved_data(self): self.assertTrue(self.user) def test_firstname_saved(self): self.assertEqual(self.user.firstname, self.form.data['firstname']) def test_lastname_saved(self): self.assertEqual(self.user.lastname, self.form.data['lastname']) def test_date_of_birth(self): self.assertEqual(self.user.profile.date_of_birth, self.form.data['date_of_birth'])
def register(request): if request.method == 'POST': form = UserRegistrationForm(request.POST or None) if form.is_valid(): new_user = form.save(commit=False) new_user.set_password(form.cleaned_data['password']) new_user.save() Profile.objects.create(user=new_user) return redirect('sell:home') else: form = UserRegistrationForm() return render(request, 'register.html', {'form': form})
def post(self, request): user_registration_form = UserRegistrationForm(request.POST) if user_registration_form.is_valid(): user_registration_form.save() username = user_registration_form.cleaned_data.get('username') raw_password = user_registration_form.cleaned_data.get('password1') first_name = user_registration_form.cleaned_data["first_name"] last_name = user_registration_form.cleaned_data["last_name"] messages.success(request, f"Congrats, { first_name }, { last_name }, you've made an account!") user = authenticate(username=username, password=raw_password) login(request, user) return HttpResponseRedirect(reverse_lazy('blog:blog-list')) context = { 'user_registration_form': user_registration_form } return render(request, 'user/register.html', context)
def register(request): if request.method == "POST": form = UserRegistrationForm(request.POST) if form.is_valid(): new_user = form.save(commit=False) new_user.set_password(form.cleaned_data["password"]) new_user.save() messages.success(request, 'User created!') return redirect("accounts:login") else: form = UserRegistrationForm() context = { "form": form, } return render(request, "accounts/register.html", context)
def user_registration(request): if request.method == "POST": link = make_password( str(request.POST["email"][0:5]) + request.POST["first_name"]) c_link = link.replace("+", "") verify_link = "http://127.0.0.1:8000/link_created/?link=" + c_link form = UserRegistrationForm(request.POST) if form.is_valid(): f = form.save(commit=False) f.first_name = request.POST['first_name'] f.last_name = request.POST['last_name'] f.email = request.POST['email'] f.password = make_password(request.POST["password"]) f.verify_link = c_link f.active = 0 f.save() mail_sending(f.first_name, f.email, verify_link) return render(request, "user_registration.html", {'success': True}) else: return render(request, "user_registration.html", {'fail': True}) return render(request, "user_registration.html")