views:

32

answers:

1
        <?php
        session_start();
        $_SESSION['username'] = "johndoe" // Must be already set
        ?>

How to write equivalent code for the above in django

+4  A: 

When SessionMiddleware is activated, each HttpRequest object – the first argument to any Django view function – will have a session attribute, which is a dictionary-like object. You can read it and write to it.

Read more ...


Example:

def login(request):
    m = Member.objects.get(username=request.POST['username'])
    if m.password == request.POST['password']:
        request.session['member_id'] = m.id
        return HttpResponse("You're logged in.")
    else:
        return HttpResponse("Your username and password didn't match.")
The MYYN