def user_register(request): registered = False if request.method == "POST": user_form = UserForm(request.POST) profile_form = UserProfileInfoForm(request.POST) if user_form.is_valid() and profile_form.is_valid( ) and user_form.cleaned_data['password'] == user_form.cleaned_data[ 'confirm_password']: user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() messages.add_message(request, messages.SUCCESS, 'You have successfully registered yourself') registered = True return HttpResponseRedirect(reverse('user_login'), {'messages': messages}) elif user_form.cleaned_data['password'] != user_form.cleaned_data[ 'confirm_password']: user_form.add_error('confirm_password', 'The passwords do not match') else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'basic_app/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'messages': messages })
def register(request): registered=False if request.method=="POST": user_form=UserForm(data=request.POST) profile_form=UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user=user_form.save(commit=False) username=user_form.cleaned_data['username'] password=user_form.cleaned_data['password'] try: validate_password(password,user) except ValidationError as e: user_form.add_error('password',e) return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered}) user.set_password(user.password) user.save() profile=profile_form.save(commit=False) profile.user=user if 'profile_pics' in request.FILES: profile.profile_pics=request.FILES['profile_pics'] profile.save() registered=True else: print(user_form.errors, profile_form.errors) else: user_form=UserForm() profile_form=UserProfileInfoForm() return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,'registered':registered})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) print('data: ', request.POST) if user_form.is_valid() \ and profile_form.is_valid() \ and user_form.cleaned_data['password'] == user_form.cleaned_data['confirm_password']: print('files: ', request.FILES) user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: print('Found file: ', request.FILES['profile_pic']) profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }) elif user_form.cleaned_data['password'] != user_form.cleaned_data[ 'confirm_password']: user_form.add_error('confirm_password', 'The passwords do not match') return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }) elif len(user_form.cleaned_data['password']) < 5: user_form.add_error('password', 'The password length must be at least 6') return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }) else: print('user_form.errors: ', user_form.errors) print('profile_form.errors: ', profile_form.errors) return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })