def 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: user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_picture' in request.FILES: profile.profile_picture = request.FILES['profile_picture'] profile.save() registered = True else: print(user_form.errors,profile_form.errors) else: user_form = UserForm(request.POST) profile_form = UserProfileInfoForm(request.POST) return render(request,'user_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) if user_form.is_valid(): user = user_form.save() # set the password as an actual password - django hashes this pw user.set_password(user.password) user.save() registered = True else: print(user_form.errors) else: user_form = UserForm return render(request, 'user_app/register.html', { 'user_form': user_form, 'registered': registered })
def signup(request): if (request.method == 'POST'): form = UserForm(request.POST) if form.is_valid(): form.save() return redirect('donate_app:donateform') else: print(form.errors) return HttpResponse('Form not Valid')
def register(request): registered = False if request.method == 'POST': try: user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) except Exception as e: raise AttributeError( "UserProfileInfoForm has no 'request' attribute") try: if user_form.is_valid() and profile_form.is_valid(): 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: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True except: raise OSError(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) else: user_form = UserForm profile_form = UserProfileInfoForm cntx = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'user_app/registration.html', context=cntx) else: user_form = UserForm profile_form = UserProfileInfoForm cntx = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'user_app/registration.html', context=cntx)
def post(self, request): self.form = UserForm(request.POST) custom_form = CustomUserCreationForm(data=request.POST) if self.form.is_valid(): user = self.form.save() user.set_password(self.form.cleaned_data['password']) user.save() profile = custom_form.save(commit=False) profile.user = user profile.photo = request.FILES.get('photo') profile.save() return HttpResponseRedirect('/users/login/') return render(request, 'user_app/signup.html', {'error': self.form.non_field_errors(), 'form': self.form})
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() # Hashing the password user.set_password(user.password) user.save() # Not commit to avoid overwriting profile = profile_form.save(commit=False) # For the OneToOne relationship with user profile.user = user if 'profile_pic' in request.FILES: # request.FILES is acting as a dictionary profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'user_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # user_form = UserForm() # user_info = UserInfo() registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) user_info = UserInfo(data=request.POST) if user_form.is_valid() and user_info.is_valid(): user = user_form.save() user.set_password(user.password) user.save() info = user_info.save(commit=False) info.user = user if 'profile_pic' in request.FILES: info.profile_pic = request.FILES['profile_pic'] info.save() registered = True else: print('Failed') else: user_form = UserForm() user_info = UserInfo() return render(request, 'user_app/registration.html', { 'user_form': user_form, 'user_info': user_info, 'registered': registered })
def register(request): registered = False if request.method == 'POST': # Get info from "both" forms # It appears as one form to the user on the .html page user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # Check to see both forms are valid if user_form.is_valid() and profile_form.is_valid(): # Save User Form to Database user = user_form.save() # Hash the password user.set_password(user.password) # Update with Hashed password user.save() # Now we deal with the extra info! # Can't commit yet because we still need to manipulate profile = profile_form.save(commit=False) # Set One to One relationship between # UserForm and UserProfileInfoForm profile.user = user # Check if they provided a profile picture if 'profile_pic' in request.FILES: print('found it') # If yes, then grab it from the POST form reply profile.profile_pic = request.FILES['profile_pic'] # Now save model profile.save() # Registration Successful! registered = True else: # One of the forms was invalid if this else gets called. print(user_form.errors,profile_form.errors) else: # Was not an HTTP post so we just render the forms as blank. user_form = UserForm() profile_form = UserProfileInfoForm() # This is the render and context dictionary to feed # back to the registration.html file page. return render(request,'user_app/registration.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
class SignUpView(View): # model = User # form_class = UserForm # template_name = 'user_app/signup.html' # def get_success_url(self): # return HttpResponseRedirect((reverse('home_app:home_url'))) # def post(self, request, *args, **kwargs): # if request.POST.get('send_button'): # return super(SignUpView, self).post(request, *args, **kwargs) # else: # return HttpResponseRedirect(reverse('home_app:home_url')) form = UserForm() def get(self, request): if request.user.is_authenticated: return HttpResponseRedirect('/article/articles/') custom_form = CustomUserCreationForm() return render(request, 'user_app/signup.html', {'form': self.form, 'custom_form': custom_form}) def post(self, request): self.form = UserForm(request.POST) custom_form = CustomUserCreationForm(data=request.POST) if self.form.is_valid(): user = self.form.save() user.set_password(self.form.cleaned_data['password']) user.save() profile = custom_form.save(commit=False) profile.user = user profile.photo = request.FILES.get('photo') profile.save() return HttpResponseRedirect('/users/login/') return render(request, 'user_app/signup.html', {'error': self.form.non_field_errors(), 'form': self.form})
def register(request): # check if user is registered registered = False if request.method == 'POST': # grab information from user form user_form = UserForm(data=request.POST) # grab information from profile info form profile_form = UserProfileInfoForm(data=request.POST) # check if both forms are valid if user_form.is_valid() and profile_form.is_valid(): # grab user form and save it to database user = user_form.save() # hash password with set method user.set_password(user.password) # save hashed password user.save() profile = profile_form.save(commit=False) # define one to one relationship profile.user = user # check for proile picture if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] # save model profile.save() # set user as registered registered = True else: # if either form is invalid print error print(user_form.errors, profile_form.errors) # if user never posts any information to forms, set user and profile forms else: user_form = UserForm() profile_form = UserProfileInfoForm() # return forms return render( request, 'user_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def signupform(request): form = UserForm() context = {'form': form} return render(request, 'signup.html', context)