-3

It is my login page:

  {% if user.is_authenticated %}
     <a href="django-sb-admin" id="django-sb-admin">Admin</a>
        <script type="text/javascript">
            window.location.href = document.getElementById("django-sb-admin").href;
        </script>
    {% else %}
        <form method="POST" class="post-form">{% csrf_token %}
        {{ loginform.as_p }}
        <button type="submit" class="btn-default">Log in</button>
        </form>
    {% endif %}

How I can automatically redirect to 'django-sb-admin' page when user authenticated in Django 1.9? Code above it's works but I wouldn't use JavaScript for this.

1 Answers1

2

You should redirect in the view, not the template.

from django.shortcuts import redirect

def login(request):
    if request.user.is_authenticated():
        return redirect('/next-url/')
    ...
Alasdair
  • 298,606
  • 55
  • 578
  • 516