def post(self, request, *args, **kwargs): instance = get_object_or_404(User, id=request.user.id) if request.method == 'POST': form = UserForm(request.POST or None, instance=instance) if form.is_valid(): instance.username = form.cleaned_data.get('username') instance.first_name = form.cleaned_data.get('first_name') instance.last_name = form.cleaned_data.get('last_name') instance.email = form.cleaned_data.get('email') instance.save() logout(request) login(request, instance, backend='django.contrib.auth.backends.ModelBackend') password = form.cleaned_data.get('password') if password != '': instance.set_password(password) instance.save() messages.success(request, _('Your informations successfully updated!')) return redirect(reverse('settings:settings_security_view')) else: messages.error(request, form.errors) return redirect(reverse('settings:settings_security_view')) else: form = UserForm(request.POST or None, instance=instance) return render(request, 'settings_security.html', {'user_form': form})
def signup(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid: form.save() return index(request) else: print('ERROR: FORM INVALID') return render(request, 'signup/signup.html', {"form": UserForm()})
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False args = {} args['username'] = auth.get_user(request).username # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # 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're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() args['user_form'] = user_form args['profile_form'] = profile_form args['registered_form'] = registered return render(request, 'register.html',args )
def home2(request): context = _get_statistics_context() if request.user.is_anonymous(): if request.method == "POST": form = UserForm(request.POST, request.FILES) if form.is_valid(): profile = form.save() user = authenticate(username=profile.user.email) login(request, user) return HttpResponseRedirect(reverse('welcome')) else: context['form'] = form else: context['form'] = UserForm() return render_with_context(request, 'home_2.html', context) else: return HttpResponseRedirect(reverse('welcome'))
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, 'signup/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def user(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = UserProfileForm(request.POST, instance=request.user.profile) print "POST DATA:" if user_form.is_valid() and profile_form.is_valid(): user_form.save() return redirect('home') else: user_form = UserForm(instance=request.user) profile_form = UserProfileForm(instance=request.user.profile) print "GET DATA:" return render(request, 'profiles/user_profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def update_profile(request,id): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = SignUpForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() #messages.success(request, _('Your profile was successfully updated!')) return redirect('settings:profile') else: #messages.error(request, _('Please correct the error below.')) print "error" else: user_form = UserForm(instance=request.user) profile_form = SignUpForm(instance=request.user.profile) return render(request, 'profiles/profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) if not self.request.user.is_authenticated: raise PermissionDenied context['user_form'] = UserForm(instance=self.request.user) return context