Answer the question
In order to leave comments, you need to log in
How to allow certain actions only to authorized users?
Added the ability to log in and register, I'm trying to make the possibility of certain actions only for authorized ones. Here is the code:
def deleteResult(request, item_id):
if request.user.is_authenticated:
try:
item = Results.objects.get(item_id=item_id)
item.delete()
return HttpResponseRedirect("/results")
except Items.DoesNotExist:
return HttpResponseNotFound("<h2>Result not found</h2>")
request.user.is_authenticated
to user.is_authenticated
, the effect is the same. How to get rid of the error?
Answer the question
In order to leave comments, you need to log in
If the user is not authorized, the function returns None, add something there, for example, a redirect to the authorization page
def deleteResult(request, item_id):
if request.user.is_authenticated:
try:
item = Results.objects.get(item_id=item_id)
item.delete()
return HttpResponseRedirect("/results")
except Items.DoesNotExist:
return HttpResponseNotFound("<h2>Result not found</h2>")
return HttpResponseRedirect('/register')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question