leemzoon
leemzoon

Reputation: 75

HttpResponseRedirect not effect in a method

The original code looks like this,it works.

def my_index(request):
    global account
    if request.session.get('account',False):
        account=request.session['account']
    else:
        return HttpResponseRedirect("http://login.passport.com/");
    print "login check pass"
    ...

when I try to put the checking lines into a method, it goes wrong.

The terminal prints Pos:A and Pos:B,but the page did't redirect to login.passport.com

It goes on to print "login check pass"

I've tried pdb.set_trace(),it goes through the HttpResponseRedirect,but I still can't find anything wrong.

Somebody give me a clue?

def login_check(request):
    global account
    if request.session.get('account',False):
        account=request.session['account']
    else:
        print "Pos:A"
        return HttpResponseRedirect("http://login.passport.com/");
        print "Pos:B"

def my_index(request):
    global account
    login_check(request)
    print "login check pass"
    ...

Upvotes: 0

Views: 76

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599450

You don't do anything with the result of login_check within my_index. You need to return it from there too.

Two other points: print Pos:B will never be reached; more seriously, you should never use global variables to store per-request state in Django. A process may last for many requests, so it's dangerous to keep variables that only relate to the information in a single request.

Upvotes: 1

Related Questions