def get(self, request, pk, *args, **kwargs): user = shortcuts.get_object_or_404(auth_models.User, pk=pk, profile__isnull=False) context = { 'user_form': forms.UserUpdateForm(instance=user), 'profile_form': forms.ProfileForm(instance=user.profile) } return shortcuts.render(request, self.template_name, context)
def test_user_update_form_can_detect_invalid_data(self): data = { 'username': None, 'email': '*****@*****.**', 'password1': 'testiNG321', 'password2': 'testiNG321' } form = f.UserUpdateForm(instance=self.user, data=data) self.assertFalse(form.is_valid())
def test_user_update_form_can_validate_valid_info(self): data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'testiNG321', 'password2': 'testiNG321' } form = f.UserUpdateForm(instance=self.user, data=data) self.assertTrue(form.is_valid())
def profile(request): if request.method == "POST": u_form = forms.UserUpdateForm(request.POST,instance=request.user) p_form = forms.ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() messages.success(request,f'Your account has been updated!') return redirect('profile') else: u_form = forms.UserUpdateForm(instance=request.user) p_form = forms.ProfileUpdateForm(instance=request.user.profile) context = { 'u_form':u_form, 'p_form':p_form } return render(request,'users/profile.html',context)
def update_profile_view(request): if request.method == 'POST': user_update_form = forms.UserUpdateForm(request.POST, instance=request.user) user_profile_update_form = forms.UserProfileUpdateForm( request.POST, request.FILES, instance=request.user.userprofile) if user_update_form.is_valid and user_profile_update_form.is_valid: user_update_form.save() user_profile_update_form.save() messages.success(request, f'Your profile has been updated successfully!') return redirect('users:profile') else: user_update_form = forms.UserUpdateForm(instance=request.user) user_profile_update_form = forms.UserProfileUpdateForm( instance=request.user.userprofile) return render( request, 'users/update_profile.html', { 'user_update_form': user_update_form, 'user_profile_update_form': user_profile_update_form })
def home(request): if request.method == 'POST': u_form = f.UserUpdateForm(request.POST, instance=request.user) if u_form.is_valid(): u_form.save() messages.success( request, f'{request.user} information updated successfully') return redirect('WebApp-home') else: messages.error(request, f'{request.user} information could not be updated') else: # print(str(request.user) == 'AnonymousUser') if str(request.user) == 'AnonymousUser': u_form = f.UserUpdateForm() else: u_form = f.UserUpdateForm(instance=request.user) context = {'u_form': u_form} return render(request, 'WebApp/home.html', context)
def post(self, request, pk, *args, **kwargs): user = shortcuts.get_object_or_404(auth_models.User, pk=pk, profile__isnull=False) user_form = forms.UserUpdateForm(request.POST, instance=user) profile_form = forms.ProfileForm(request.POST, request.FILES, instance=user.profile) if user_form.is_valid() and profile_form.is_valid(): with transaction.atomic(): user_form.save() profile_form.save() return shortcuts.redirect('users:user_detail', pk=user_form.instance.id) else: context = { 'user_form': user_form, 'profile_form': profile_form } return shortcuts.render(request, self.template_name, context)
def test_UserUpdateForm(self, user1): data = { "email": "*****@*****.**", } form = forms.UserUpdateForm(data=data) assert form.is_valid()