How can I prevent a logged in user from accessing a 'Log In' or 'Sign Up' page in Django version 1.9?
<p>I'm pretty new to Python.</p>

<p>My problem is that I want to restrict users who are already logged in from being able to visit the log in and sign up pages.</p>

<p>Essentially, what I'm looking for is something like the @login_required decorator, that will allow access to these pages for users who are <strong>not</strong> logged in.</p>

<p>So far, I have </p>

<ul>
<li>Looked at other SO questions such as <a href="http://stackoverflow.com/questions/15263102/django-registration-how-to-prevent-logged-in-user-from-registering">Django-Registration: How to prevent logged in user from registering?</a>, however I've tried the solution here, and it does not work for me.</li>
<li>I've searched through the Django Documentation, Django Girls and other websites that offer tutorials for learning Django.</li>
</ul>

<p>Here's my views.py for the log in:</p>

<pre><code>def login_view(request): # Login View to begin user session
print(request.user.is_authenticated())
if request.method == 'POST':
    form = UserLogInForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user is not None:
                login(request, user)
                print(request.user.is_authenticated())
                return HttpResponseRedirect('/')
        else:
            form = UserLogInForm()
        return render(request, 'login.html', {'form': form})
else:
    form = UserLogInForm()
return render(request, 'login.html', {'form': form})
</code></pre>

<p>and the sign up:</p>

<pre><code>class signup_view(View):
    form_class = UserSignUpForm
    template_name = 'signup.html'
    # Blank form is requested
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})
        # Form is filled in and posted to DB - Process the Data
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():
            # Further checks before committing object to the DB
            # Cleaned Data
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            email = form.cleaned_data['email']
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(first_name=first_name,
                                            last_name=last_name,
                                            email=email,
                                            username=username,
                                            password=password
             )
             user.set_password(password)
             user.save()

             user = authenticate(email=email, username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')

    return render(request, self.template_name, {'form': form})
</code></pre>

<p>If anybody knows any solutions then that would be amazing, thank you.</p>

<p>Since you are using a class-based view, you can use the <a href="https://docs.djangoproject.com/en/1.10/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin">UserPassesTestMixin</a> and in the check method test that the user is anonymous:</p>

<pre><code>class signup_view(UserPassesTestMixin, View):
    ...
    def test_func(self):
        return self.request.user.is_anonymous()
</code></pre>

<p>You can do a similar thing for the other view using the <code>@user_passes_test</code> decorator.</p>

python django