Reputation: 75
I have a book page. On this page is the button "In favorite". If the user clicks on the button and is authenticated, it will use addBookmark view to add a new object into the database(and just reload the book page). However, if the user isn't authenticated, it'll redirect to the login page firstly.
@login_required
def addBookmark(request, slug):
book = Book.objects.get(slug=slug)
if BookMark.objects.filter(user=request.user, book=book).exists():
bookMark = BookMark.objects.get(user=request.user, book=book)
bookMark.delete()
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
newBookMark = BookMark.objects.create(user=request.user, book=book)
newBookMark.save()
return HttpResponseRedirect(request.META.get("HTTP_REFERER"))
The problem: When a user is redirected to the login page, the next URL will just add a new object in db and reload the page, but this is the login page. How can I redirect users back to the book page if the user isn't authenticated firstly?
Upvotes: 1
Views: 158
Reputation:
Firstly, remove @login_required
.
You can redirect user manually like this:
if not request.user.is_authenticated:
return redirect("bookmark_url")
Upvotes: 1