I’m trying to limit a page so only the person whose page matches the URL can access it:
urlconf
url(r'^u/(?P<name>[-\w\d_]+)/edit/$', 'useraccounts.views.user_profile_edit')
views.py
def user_profile_edit(request, name):
if request.user.is_authenticated() and request.user == name:
username = User.objects.get(username=name)
return render(request, 'useredit.html', {'user': username})
Printing out the contents of print request.user and print name upon page load to console shows the two are the same in my case (both are root). However, print bool(request.user == name) returns False, and as a result the view doesn’t progress.
What is going on, and if applicable, is there a more elegant way to go about doing this?
You did it wrong.
request.useris an object, whilenameis a string.Rewrite your condition as follows: