def post(self, request): # Attempt to grab info from the raw form information. form = UserProfileForm(request.POST, request.FILES) print(f"the image filename is {request.FILES}") # if the two forms are valid... if form.is_valid(): # Now sort out the UserProfile instance # Since we need to set the user attribute ourselves # we set commit=False. This delays saving the model # until we are ready to avoid integrity problems user_profile = form.save(commit=False) user_profile.user = request.user #Did the user provide a profile picture? if so we need to get it from the input form and # put it in the UserProfile model. #if 'picture' in request.FILES: # user_profile.picture = request.FILES['picture'] #Now we save the UserProvile model instance user_profile.save() # registration was successful return redirect(reverse('Rango:index')) else: # Invalid form or forms - mistakes or something else? #print problems to the terminal print(form.errors) #Render the template depending on the context. return render(request, 'Rango/profile_registration.html', context = {'form': form})
def profile(request, username): try: user = User.objects.get(username=username) except User.DoesNotExist: return redirect('Rango:index') userprofile = UserProfile.objects.get_or_create(user=user)[0] print(userprofile) form = UserProfileForm({ 'website': userprofile.website, 'picture': userprofile.picture }) if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES, instance=userprofile) if form.is_valid(): print(form.is_valid()) form.save() return redirect('Rango:profile', user.username) else: print(form.errors) return render(request, 'Rango/profile.html', { 'userprofile': userprofile, 'selecteduser': user, 'form': form })
def getUserDetails(self, username): # django.contrib.auth.User is an instance from database sel_user = get_object_or_404(User, username=username) userProfile = UserProfile.objects.get_or_create(user=sel_user)[0] form = UserProfileForm({'website': userProfile.website, 'picture': userProfile.picture}) return(sel_user, userProfile, form)
def get(self, request): # we come here with blank form for user to fill in OR because user has filled out and we post to DB form = UserProfileForm() #Render the template depending on the context. return render(request, 'Rango/profile_registration.html', context = {'form': form})
def post(self, request, username): (sel_user, userProfile, form) = self.getUserDetails(username) # Now sort out the UserProfile instance # we have user attribute so dont need to wait to save. Take new stuff and put in old form instance #this is an update rather than a new save form = UserProfileForm(request.POST, request.FILES, instance=userProfile) if form.is_valid(): form.save(commit=True) # not clear on why this works, if there is no param, you have to do a reverse ?? return redirect('Rango:profile', sel_user.username) else: #not sure how to test this with an incorrect form??? print(form.errors) context_dict = {'form': form, 'selected_user': sel_user, 'user_profile':userProfile} return render(request, 'Rango/profile.html', context = context_dict)
def register_profile(request): form = UserProfileForm() if request.method == 'POST': form = UserProfileForm(request.POST, request.FILES) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() return redirect('index') else: print form.errors return render(request, 'Rango/profile_registration.html', {'form': form})