How does UserPassesTestMixin in django work?

2024/7/7 6:00:48

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.

Answer

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.

https://en.xdnf.cn/q/120168.html

Related Q&A

How to extract URL from HTML anchor element using Python3? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Why does using open(filename) fail with filename is not defined?

I want to open a text file containing a column of words and create a list or, alternatively, a string containing these words. Why do I get this error: >>> with open(some_file.txt, r) as some_f…

Read Excel file which has one of the column as Hyperlink through python

I have to read an Excel file Using python. By the time I use xl = pd.ExcelFile("abc.xlsx")The column values which had hyperlink assigned to it becomes a simple number without any hyperlink.Is…

How can I create n number of files in python?

say user gives a number n=3 then I have to create 3 files dynamically. How will I do that? What can be the names of those files. Specifically I want n number of .jpg file created.

For loop doesnt append info correctly into 2D array

I have created an empty 2D array. When I try to add stuff inside of it, it doesnt do so properly. Each index contains the appropriate info, but for some reason, carries the info from the previous into …

How to merge two strings in python?

I need to merge strings together to create one string. For example the strings for "hello" that need to be combined are: [H----], [-E---], [--LL-], and [----O]This is the current code I have …

ValueError: too many values to unpack (expected 3)?

I have been having issues with the code I am trying to right with the model I am trying to code the following error has appeared and being a relative novice I am unsure of how to resolve it.ValueError …

Finding a hello word in a different string, which it has hello in it

I should find a hello word in a string, which I gave it from input. Here is the code that I currently have, but I cannot match hello with the character list.mylist = it can be any letter plus hello in …

Building Permutation with Python

Im trying to write a programme to get all permutations of a string of letter using recursion. As Im a beginner in Python, I learnt about recursion with examples like Fibonacci Number and Factorial. I u…

Python: Is There a builtin that works similar but opposite to .index()?

Just a forewarning: I just recently started programming and Python is my first language and only language so far.Is there a builtin that works in the opposite way of .index()? Im looking for this beca…