• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

django authentication redirect

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Stratus_ss

Overclockix Snake Charming Senior, Alt OS Content
Joined
Jan 24, 2006
Location
Ontario Canada
Hi guys,

I have been constructing a very basic django website that is supposed to log users in and allow them to upload/download/delete files or other small tasks.

I have got the website binding to Active Directory, and I have also put custom middleware in to require all pages to authenticate. This is all working fine.

What I would like is for a user to be able to click on the upload button, and if they are not authenticated, be sent to the login page, then upon logging in get dropped back at the upload page.

Currently the redirect to the login page is working as expected. I am not quite sure how to proceed with the second part (redirecting to the original clicked link)

here is my views.py

Code:
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from django.contrib.auth.forms import UserCreationForm
from forms import MyRegistrationForm


def login(request):
    if not request.user.is_authenticated():
        c = {}
        c.update((csrf(request)))
        return render_to_response('login.html', c)
    else:
        return render_to_response('loggedin.html')

def auth_view(request):
    username = request.POST['username'],
    password = request.POST['password'],
    user = auth.authenticate(username=username[0], password=password[0])
    print(username[0])
    print(password[0])
    print(user)
    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/loggedin')
    else:
        return HttpResponseRedirect('/accounts/invalid')

def loggedin(request):
    return render_to_response('loggedin.html', {'full_name': request.user.username})

def invalid_login(request):
    return render_to_response('invalid_login.html')

def logout(request):
    auth.logout(request)
    return render_to_response('login.html')

def register_user(request):
    if request.method == 'POST':
        #form = UserCreationForm(request.POST)
        form = MyRegistrationForm(request.POST)
        if form.is_valid(): 
            form.save()
            return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))
    #args['form'] = UserCreationForm()
    args['form'] = MyRegistrationForm()

    return render_to_response('register.html', args)

def register_success(request):
    return render_to_response('register_success.html')

Base.html
Code:
{% load staticfiles %}
<!DOCTYPE html>
<head>
    <title>{% block title %}My base template{% endblock %}</title>
        <link rel="stylesheet" type="text/css" href="{% static "assets/css/default.css" %}">
</head>
<body>
    <div id='page'>

    <div id="sidebar">
    {% block sidebar %}
    <ul>
        <li><a href="/admin/">Admin</a></li>
        <li><a href="/articles/all">Articles<a></li>
        <li><a href="/upload">Upload Warfiles<a></li>
    </ul>
    {% endblock %}
    </div>
    <div id='content'>
        {% block content %}This is the content area{% endblock %}
        <img src="{% static "assets/images/energy-arc.jpg" %}" />
    </div>
    </div>

</body>
</html>

login.html

Code:
{% extends "base.html" %}

{% block content %}

    {% if form.errors %}
        <p class="error">Sorry, thats not a valid username or password</p>
    {% endif %}

    <form action="/accounts/auth/" method="post">{% csrf_token %}
        <label for="username">User name:</label>
        <input type="text" name="username" value="" id="username">
        <label for="password">Password:</label>
        <input type="password" name="password" value="" id="password">
        <input type="submit" value="login" />
    </form>

{% endblock %}

Can anyone steer me in the right direction?
 

pcgamer4life

Member
Joined
Dec 30, 2004
My first instinct would be to store the referring page in a session variable, cookie, query string parameter, etc, when you get to the login page. If that variable is null, then redirect them to the homepage after logging in, otherwise redirect them from whence they came.