In my project i have UserUpdateView like this:
<code>class UserUpdateView(AuthenticationMixin, AuthorizationMixin, SuccessMessageMixin, UpdateView):
'''Update User info(username, full/second name, password)'''
model = User
form_class = UserForm
template_name = 'form.html'
permission_denied_message = _("You can't change this profile, this is not you")
permission_denied_url = reverse_lazy('users-detail')
success_message = _('User Profile is successfully changed')
success_url = reverse_lazy('users-detail')
extra_content = {
'title': _('Update user'),
'button_text': _('Update'),
}
</code>
<code>class UserUpdateView(AuthenticationMixin, AuthorizationMixin, SuccessMessageMixin, UpdateView):
'''Update User info(username, full/second name, password)'''
model = User
form_class = UserForm
template_name = 'form.html'
permission_denied_message = _("You can't change this profile, this is not you")
permission_denied_url = reverse_lazy('users-detail')
success_message = _('User Profile is successfully changed')
success_url = reverse_lazy('users-detail')
extra_content = {
'title': _('Update user'),
'button_text': _('Update'),
}
</code>
class UserUpdateView(AuthenticationMixin, AuthorizationMixin, SuccessMessageMixin, UpdateView):
'''Update User info(username, full/second name, password)'''
model = User
form_class = UserForm
template_name = 'form.html'
permission_denied_message = _("You can't change this profile, this is not you")
permission_denied_url = reverse_lazy('users-detail')
success_message = _('User Profile is successfully changed')
success_url = reverse_lazy('users-detail')
extra_content = {
'title': _('Update user'),
'button_text': _('Update'),
}
User model like this:
<code>class User(AbstractUser):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
def __str__(self):
return self.get_full_name()
</code>
<code>class User(AbstractUser):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
def __str__(self):
return self.get_full_name()
</code>
class User(AbstractUser):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
def __str__(self):
return self.get_full_name()
And test for UserUpdateView:
<code>class UpdateUserTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username='tota123', first_name='Andrey', last_name='Totavich', password='lexA456132')
def test_update_user(self):
self.client.login(username='tota123', password='lexA456132')
response = self.client.post(
reverse('user-update', kwargs={'pk': self.user.pk}),{
'username': 'tota321',
'first_name': 'Sergey',
'last_name': 'Simo',
'password1': 'lexA456132',
'password2': 'lexA456132'
})
self.assertEqual(response.status_code, 302) # Redirect after successful update
self.assertEqual(self.user.username, 'tota321')
</code>
<code>class UpdateUserTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username='tota123', first_name='Andrey', last_name='Totavich', password='lexA456132')
def test_update_user(self):
self.client.login(username='tota123', password='lexA456132')
response = self.client.post(
reverse('user-update', kwargs={'pk': self.user.pk}),{
'username': 'tota321',
'first_name': 'Sergey',
'last_name': 'Simo',
'password1': 'lexA456132',
'password2': 'lexA456132'
})
self.assertEqual(response.status_code, 302) # Redirect after successful update
self.assertEqual(self.user.username, 'tota321')
</code>
class UpdateUserTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username='tota123', first_name='Andrey', last_name='Totavich', password='lexA456132')
def test_update_user(self):
self.client.login(username='tota123', password='lexA456132')
response = self.client.post(
reverse('user-update', kwargs={'pk': self.user.pk}),{
'username': 'tota321',
'first_name': 'Sergey',
'last_name': 'Simo',
'password1': 'lexA456132',
'password2': 'lexA456132'
})
self.assertEqual(response.status_code, 302) # Redirect after successful update
self.assertEqual(self.user.username, 'tota321')
The tests result:
<code>self.assertEqual(self.user.username, 'tota321')
AssertionError: 'tota123' != 'tota321'
- tota123
? --
+ tota321
? ++
</code>
<code>self.assertEqual(self.user.username, 'tota321')
AssertionError: 'tota123' != 'tota321'
- tota123
? --
+ tota321
? ++
</code>
self.assertEqual(self.user.username, 'tota321')
AssertionError: 'tota123' != 'tota321'
- tota123
? --
+ tota321
? ++
How can i solve this problem & why model of user dont change? When i run my project localy – i can change user info, and it work, UserUpdateView works correctly. What i missing ?
New contributor
Andrei Emelianenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1