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() 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() registered=True else: print(user_form.errors,profile_form.errors) else: user_form=UserForm() profile_form=UserProfileInfoForm() return render(request,'basic_app/SignUp_FormValidation.html',{'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
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(commit=False) user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # oneToOne if 'profile_pic' in request.FILES: 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() context = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} return render(request, 'basic_app/registration.html', context)
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() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user 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() registered = True else: print(user_form.errors,profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request,"basic_app/registration.html",context={'user_form':user_form, 'profile_form' : profile_form, 'registered': registered})
def register(request): registered=False if request.method=="POST": profile_form=UserProfileInfoForm(data=request.POST) user_form=UserForm(data=request.POST) if profile_form.is_valid() and user_form.is_valid(): user=user_form.save() user.set_password(user.password)#this line hashes the password into unique code user.save() profile=profile_form.save(commit=False) #That's useful when you get most of your model data from a form, but need to populate some null=False fields with non-form data. Saving with commit=False gets you a model object, then you can add your extra data and save it. profile.user=user#this line of code means that, the profile will not override the data in user form but will form a OneToOneField relation with user. if 'profile_pic' in request.FILES: profile.profile_pic=request.FILES['profile_pic']#sutax code for saving profile_pic 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 user_form = UserForm() profile_form = UserProfileInfoForm() 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(): # for user user = user_form.save() user.set_password(user.password) # set hashing password user.save() profile = profile_form.save( commit=False) # 表單.save() 會回傳 該表單連結的 Data Model profile.user = user if 'profile_pics' in request.FILES: # for image files profile.profile_pics = request.FILES[ 'profile_pics'] # profile_pics 是剛剛前面在 建立 models 針對 pic的設定 profile.save() registered = True else: print(user_form.errors, profile_form.errors) context = { "registered": registered, "user_form": user_form, "profile_form": profile_form } return render(request, 'basic_app/registration.html', context=context)
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() 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 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=UserProfile(data=request.POST) if(user_form.is_valid() and profile_form.is_valid()): user=user_form.save() user.set_password(user.password) #Hashing the 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 else: print(user_form.errors,profile_form.errors) else: user_form=UserForm() profile_form=UserProfile() 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) 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_pics = request.FILES['profile_pics'] profile.save() registered = True else: print(user_form.errors, profile_form.error) 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) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() # This is about "PASSWORD_HASHERS" item defined within settings.py user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # OneToOne relationship user = models.OneToOneField(User) models.py if 'profile_pic' in request.FILES: 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, 'basic_app/registration.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered,})
def register(request): #aassume first user not registered 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(): #save directed to database user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) #make sure set up one to one relationship #to prevent overide the user profile.user = user if 'profile_pic' in request.FILES: 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() #registered = True 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) 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 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, "title": "SignUp" })
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_pic" in request.FILES: 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, "basic_app/registeration.html", { "user_form": user_form, "userprofile_form": profile_form, "registered": registered })
def register(request: "HttpRequest") -> HttpResponse: 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 = user_form.save() user.set_password(user.password) user.save() profile: UserProfileInfo = profile_form.save( commit=False ) # To avoid saving to the database directly and perform operations before. profile.user = user if "profile_pic" in request.FILES: 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, "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) 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) # the reason for not commit is we dont want to overwrite it # the following code is build the one to one relationship profile.user = user # same type of method dealing with other files # live .csv .pdf resume something like that if 'profile_pic' in request.FILES: 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, 'basic_app/registration.html', {'user_form':user_form, 'profile_form':profile_form, '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 directly to Database user = user_form.save() # Hash the password (goes to settings.py file and sets it as hash) user.set_password(user.password) # Update (saving changes to) the database with the Hashed password user.save() # EXTRA information # Can't commit (save to database) 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: # 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) # the above just prints out the actual 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. context = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'basic_app/registration.html', context)
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, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): registered = False #Check if user clicked Register button if request.method == "POST": #we will use following values in registration.html file as template tags #Not sure ,I think here we get data from user and save it to variable #it happens after user click Register button user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) #If all data we get from user is valid then do following if user_form.is_valid() and profile_form.is_valid(): #After check data is valid we save it user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) #Not sure I think somehow we connect form results #Not sure but I think this line has some relation with line(I write it down) in models.py under UserProfileInfoForm class #user = models.OneToOneField(User) profile.user = user #Here we check if picture submited if 'profile_pic' in request.FILES: #here we assign picture to profile.profile_pic #This profile_pic are from models.py ,you can see it there #Not Sure:like profile_pic is in models.py then it connected to forms.py with UserProfileInfoForm #then connected to views.py via UserProfileInfoForm profile.profile_pic = request.FILES['profile_pic'] #Here we save profile ,no matter there is pic or not,it is outside of if profile.save() #We will use this boolen on registration.html file #so if user registered we will show 'Thank you for your registration' registered = True else: #here we print validation errors if happens print(user_form.errors, profile_form.errors) else: #This is for fresh page,I mean above code are mostly about what will happen if user clicks 'Register' button #But here we just display forms(like inputboxes,browse file and so),This form is organized by forms.py particularly by UserForm and UserProfileInfoForm #Basicly first page shows form with below code then user fills info then click button after click above code works user_form = UserForm() profile_form = UserProfileInfoForm() #This returns page,'basic_app/registration.html' is location of html #{'user_form':user_form,'profile_form':profile_form,'registered':registered} these things are basicly context ,template tags on html will use these values #example this {{ user_form.as_p }} tag in registration.html 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': # 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,'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) # # if user_form.is_valid() and profile_form.is_valid: # user = user_form.save() # user.set_password(user.password) #hashing password # user.save() # # profile = profile_form.save(commit=False) # profile.user = user #Defing one to one relation with user # # if 'profile_pic' in request.FILES: # 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() 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() 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 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 })
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): #user_form ko sedha save kar user = user_form.save() pas = user.password user.set_password(user.password) user.save() #we grabed ser information password set kiye fir save kiye database me profile = profile_form.save(commit=False) profile.user = user #one to one in models if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: #invalid print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserInfoForm() if registered == False: return render(request, 'register.html', { 'user_form': user_form, 'profile_form': profile_form }) else: check = authenticate(username=user.username, password=pas) if check: if check.is_active: login(request, check) # save in audit table a = audit(name=user.username, login_time=str(datetime.now()), logout_time='') a.save() dict = audit.objects.all() # //u can send dict to any html page to display db contents return render(request, 'other.html', { 'username': user.username, 'password': pas }) else: return HttpResponse('account not active') else: return HttpResponse( 'invalid login supplied username {} password {}!'.format( user.username, pas))
def register(request): registered = False if request.method == 'POST': # Get info from "both" forms 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 using argonpassword hasers algorithm user.set_password(user.password) user.save() profile = profile_form.save(commit=False) # Set One to One relationship between # UserForm and UserProfileInfoForm profile.user = user if 'profile_pic' in request.FILES: print('found it') 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, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): registered = False # get info from "both" forms 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(): #save User form to database user = user_form.save() # Hash the password user.set_password(user.password) # update with hashed password user.save() # now with the extra info. # can't commit=True because we still need to manipulate profile = profile_form.save(commit=False) # set One to One relationship between UserForm and UserPofileInfoForm profile.user = user # checking for any profile picture if 'profile_pic' in request.FILES: print('found it') # if yes, then grab it from the POST from reply profile.profile_pic = request.FILES['profile_pic'] # now save model profile.save() # registration successful registered = True else: 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() return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, })
def register(request): # remenber why use the register False # this is used instant of the instance stuff registered = False # checking if is a Post reqeust if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # check if it is valid if user_form.is_valid() and profile_form.is_valid(): #save the user_form first with an instance user = user_form.save() # set a password sha user.set_password(user.password) # and save all user.save() # sending it to the database but not saving yet profile = profile_form.save(commit=False) #setting all the relationship profile.user = user # check for profile pic if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] # save everything profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request=request, template_name='basic_app/registration.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): registered = False if request.method == "POST": # Get info from both forms user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # Check if 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 with the algorithms user.set_password(user.password) # Now save the password user.save() # Don't save yet profile form, wait until modified profile = profile_form.save(commit=False) # Create the One to One Relationship from the models profile.user = user # Check if profile picture is provided if "profile_pic" in request.FILES: print("Found it") profile.profile_pic = request.FILES["profile_pic"] # Save profile form to Database profile.save() # Registration successful registered = True else: print(user_form.errors, profile_form.errors) else: # It was not a HTTP Post request, so forms are render blank 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): ##default to false to check ## 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() ##hashes password ## user.set_password(user.password) user.save() ##commit false to avoid collisions ## profile = profile_form.save(commit=False) ##creates one to one relationship ## profile.user = user ##check if picture submitted ## if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True ##invalid form ## else: print(user_form.errors, profile_form.errors) ##if not set to post ## 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: HttpRequest) -> HttpResponse: 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() # Hash the password using 'set_password' method user.set_password(user.password) # Save the model to DB user.save() # Save profile form without committing 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 else: context = { 'error_message': 'There seems to be an error in the form you filled. Please retry' } print(user_form.errors) print(profile_form.errors) return render(request=request, template_name='basic_app/registration.html', context=context) else: user_form = UserForm() profile_form = UserProfileInfoForm() context = { 'registered': registered, 'user_form': user_form, 'profile_form': profile_form, } return render(request=request, template_name='basic_app/registration.html', context=context)
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() #hash the password and save it! user.set_password(user.password) user.save() profile=profile_form.save(commit=False) #one to one relationship profile.user = user #profile picture if 'profile_pic' in request.FILES: print('found it!') #link the profile_pic with the request.FILES profile.profile_pic = request.FILES['profile_pic'] #now we want to save the profile profile.save() #registration Successful registered = True #if one of them is invalid else: print(user_form.errors,profile_form.errors) else: #if method isnt POST we just render forms as blank! easy! user_form=UserForm() profile_form=UserProfileInfoForm() return render(request,'basic_app/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
def register(request): # variable to indicate if someone is registered or not registered = False if request.method == "POST": user_form = UserForm( data=request.POST) # contains the data from UserForm profile_form = UserProfileForm( data=request.POST) # contains the data from UserProfileForm # check if both the forms are valid if user_form.is_valid() and profile_form.is_valid(): # save the user information to the database user = user_form.save() # hashing the password entered in by the user user.set_password(user.password) # save the hashed password to the database user.save() # save the extended user information to the database profile = profile_form.save(commit=False) # setup the one to one relationship with User profile.user = user # check if a profile picture was uploaded if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: # both forms not valid print(user_form.errors, profile_form.errors) else: # not a post method - user did not fill in form yet, set everything up user_form = UserForm() profile_form = UserProfileForm() return render( request, "basic_app/registration.html", { "registered": registered, "user_form": user_form, "profile_form": profile_form })
def register(request): # assume not registered registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # check to see if the forms are valid if user_form.is_valid and profile_form.is_valid: # saves the user form user = user_form.save() # hashes the password user.set_password(user.password) # saves the password and user info user.save() # save the profile but not going to commit yet profile = profile_form.save(commit=False) # sets the one-to-one relationship profile.user = user # checks to see if a profile pic was uploaded # media and files are in request.FILES if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] # save profile.save() # set the registered to true cuz now they are registered registered = True else: # prints out the errors print(user_form.errors, profile_form.errors) else: # just sets the forms user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'basic_app/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def post(self,request): registered = False 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() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print(user_form.errors,profile_form.errors) 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) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: print(user_form.errors) else: user_form = UserForm() return render(request,'basic_app/registration.html',{'user_form':user_form, 'registered':registered})
def register(request): registered = False if request.method == 'POST': #form submitted user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) #check form validity if user_form.is_valid() and profile_form.is_valid(): #form is valid #save the user_form user = user_form.save() #hash the password user.set_password(user.password) #save user.save() #save the profile_form - but do not commit the changes as yet as the file & other data is not present profile = profile_form.save(commit=False) #set the profile's user profile.user = user #update profile_pic if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] #finally save the profile form profile.save() registered = True else: #form is invalid print(user_form.errors) print(profile_form.errors) else: #1st time load #create empty forms user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'basic_app/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # Assume user is not registered registered = False # Check if form submit method is post if request.method == "POST": # get form data user_form = UserForm(data=request.POST) profile_form = UserProfileFormInfo(data=request.POST) # check if forms are valid if user_form.is_valid() and profile_form.is_valid(): # grab details from base user form and save to db user = user_form.save() user.set_password(user.password) user.save() # grab details from profile form profile = profile_form.save(commit=False) profile.user = user # check if these is a picture in media files if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] # save it to db if it exists profile.save() # set registered to true so that it passes the form to template registered = True else: # print errors if there are problems print(user_form.errors, profile_form.errors) else: # if form not posted, create form variables user_form = UserForm() profile_form = UserProfileFormInfo() # render register template to allow user to register return render(request, 'basic_app/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})