How does UserPassesTestMixin in django work?

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 == int(self.kwargs['pk']). Don’t forget to catch ValueError if there’s a possibility of it not looking like an integer.

Leave a Comment