A
A
Anton2017-02-21 22:46:47
Django
Anton, 2017-02-21 22:46:47

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 @login_required(login_url='/user/login'). 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'))

But maybe there are other options for such actions?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita Konin, 2017-02-21
@jkjkjirf

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

A
Alexander Talalaev, 2017-10-28
@neuotq

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.

D
Dmitry Plotnikov, 2017-10-28
@dimap101

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>

2. issuing a response code 301 indicating the page for the redirect:
<?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 question

Ask a Question

731 491 924 answers to any question