If the user user was redirected from another page, django will provide the next
query string parameter, so that the user can be redirected back after signing in.
Add this to your settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
then, in the template, check if this parameter exists or not.
{% if 'next' in request.GET %}
Not tested, though.
See Auth docs