views.py
class ProfileEdit(UserPassesTestMixin, UpdateView):model = Userform_class = ProfileFormtemplate_name="profile/profile_new.html"def test_func(self):x = self.request.user.idprint (x)y = self.kwargs['pk']print (y)a = Trueb = Falseif self.request.user.id == self.kwargs['pk']:print (a)else:print (b)return redirect ('/login/')
enter image description here
as you can see in the output image the test condition actually satisfies.. but why does it print "False" if it's true.
The user id coming from the user in the request object is an integer, while the self.kwargs['pk']
is a string unless you do something about it. You'd see the difference if you printed repr()
of the values because the string would have quotes around it because it's extracted from the url path which itself is a string.
Try casting it with int()
before comparing like self.request.user.id == int(self.kwargs['pk'])
. Don't forget to catch ValueError
if there's a possibility of it not looking like an integer.