Answer the question
In order to leave comments, you need to log in
Access to views only for not logged in user?
Hello. There is a decorator in django that allows you to restrict access to not logged in users, it looks like this . And how it is possible to restrict access to methods to users who are logged in?
I did it like this
if request.user.is_authenticated:
return HttpResponseRedirect(reverse('default'))
Answer the question
In order to leave comments, you need to log in
You can write a decorator that will redirect logged in users.
Like this:
def redirect_if_authenticated(view_func):
@wraps(view_func)
def dispatch_wrapper(self, request, *args, **kwargs):
if request.user.is_athenticated:
return redirect('default')
return view_func(self, request, *args, **kwargs)
return dispatch_wrapper
The most stupid and easiest way, after checking authorization (I don’t know how it is with you through cookies, for example), put a header:
In short, there is an address where to redirect the browser. It is only important that there is no other output or tags before executing this command. In short, it is important that the transfer of the header to the browser to the user goes first.
Why ask the question twice?
My answer published in the second question:
Create an index.php file in the /acount folder and write the redirect in it:
1. via JS:
<script>
document.location.href = '/acount/login.php';
</script>
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://профи-продвижение.рф/acount/login.php");
exit();
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question