Hello, all.

I have a login function for my site that is supposed to test for
cookie support with request.session.set_test_cookie() and friends, but
I cannot seem to get request.session.test_cookie_worked() to return
True.. ever... Just trying to print the value of
request.session['testcookie'] resulted in a KeyError, so presumably
the problem has to do with saving the sessions somewhere between
requests.

Sessions do appear to work for the rest of my app, and the
contrib.auth.views login view behaves as predicted, but something
about my code seems to cause this lack of working-ness. I have asked
in IRC, but after trying a bunch of things I was advised to ask here.
Included are my login view, activation view, and a view I made that
contains only the cookie-related stuff.

### Here's my login function:

def login(request):
    "Displays the login form and handles the login action."
    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
    if request.POST:
        if not request.session.test_cookie_worked(): ## I added this
line to test with. It always raises the exception :\ I wasn't checking
the for cookies working before (oops!), so I didn't notice that.
            raise Exception
        form = AuthenticationForm(request.POST)
        if form.is_valid():
            # Light security check -- make sure redirect_to isn't
garbage.
            if not redirect_to or '://' in redirect_to or ' ' in
redirect_to:
                from django.conf import settings
                redirect_to = settings.LOGIN_REDIRECT_URL
            user =
authenticate(username=form.cleaned_data['username'],
password=form.cleaned_data['password'])
            if user is not None:
                if user.is_active:
                    request.session.delete_test_cookie()
                    login(request, user)
                    return HttpResponseRedirect(redirect_to)
                else:
                    return render_to_response('user_area/login.html',
{'form': form,
 
REDIRECT_FIELD_NAME: redirect_to,
 
'redirect_field_name': REDIRECT_FIELD_NAME,
 
'disabled': True},
 
context_instance=RequestContext(request))
            else:
                return render_to_response('user_area/login.html',
{'form': form,
 
REDIRECT_FIELD_NAME: redirect_to,
 
'redirect_field_name': REDIRECT_FIELD_NAME,
 
'authfail': True},
 
context_instance=RequestContext(request))
        else:
            return render_to_response('user_area/login.html', {'form':
form,
 
REDIRECT_FIELD_NAME: redirect_to,
 
'redirect_field_name': REDIRECT_FIELD_NAME,},
 
context_instance=RequestContext(request))
    else:
        form = AuthenticationForm()
        request.session.set_test_cookie()
        return render_to_response("user_area/login.html", {
            'form': form,
            REDIRECT_FIELD_NAME: redirect_to,
            'redirect_field_name': REDIRECT_FIELD_NAME,
        }, context_instance=RequestContext(request))



### Here's my activate function, which also sets a test cookie because
it doubles as a login form
def activate(request, hash):
    #u = get_object_or_404(User, userprofile__hash__exact=hash,
is_active__exact=False)
    u = get_object_or_404(User, userprofile__hash__exact=hash)
    u.is_active = True
    u.get_profile().hash = None
    u.save()

    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '')
    form = AuthenticationForm()
    request.session.set_test_cookie()
    print request.session['testcookie'] # prints 'worked'

    return render_to_response("user_area/login.html", {
        'form': form,
        'activated': True,
    }, context_instance=RequestContext(request))


### Here is the function that isolates the cookie testing stuff. It
also doesn't work the way it should
def test_cookie(request):
    if request.POST:
        if not request.session.test_cookie_worked():
            raise Exception
        request.session.delete_test_cookie()
        return render_to_response("user_area/login.html", {
                                 'activated': True,
                                 },
context_instance=RequestContext(request))
    else:
        form = AuthenticationForm()
        request.session.set_test_cookie()
        return render_to_response("user_area/test.html", {
            'form': form,
        }, context_instance=RequestContext(request))

Any help or suggestions would be much appreciated!

jose


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to