views:

32

answers:

1

I'm writing a django login view, that re-uses the generic one to do most of its heavy lifting, but I handle some details afterwards:

COMPANY_COOKIE = 'last_login_company_id'

def login(request, *args, **kwargs):
    initial_company_id = request.COOKIES[COMPANY_COOKIE] if COMPANY_COOKIE in request.COOKIES else None
    def makeCompanyAuthenticationForm(*args, **kwargs):
        kwargs.setdefault('initial', {})
        initial = kwargs['initial']
        initial['company'] = initial_company_id
        return CompanyAuthenticationForm(*args, **kwargs)

    kwargs['authentication_form'] = makeCompanyAuthenticationForm
    response = django_login(request, *args, **kwargs)

    if request.method == 'POST' and request.user.is_authenticated():
        request.session['user_menu'] = get_user_menu()
        if 'company' in request.POST:
            log.debug("Storing user company %s in cookie %s", request.POST['company'], COMPANY_COOKIE)
            response.set_cookie(COMPANY_COOKIE, request.POST['company'])
        request.session.save()
    return response

Leaving aside any comments on the mechanism of setting the default company in my custom form, I'm wondering why my COMPANY_COOKIE is not being set.

I'm using the Django debug toolbar with INTERCEPT_REDIRECTS set to True, and I can see that my log statement is being called. If I insert a pdb.set_trace() after set_cookie, I can see that response.cookies contains my company ID cookie. However, my browser does not have it. In the DjDT, I can see that my request's COOKIES variable does not contain my cookie, and in chrome's storage inspector I see the same thing; there's no last_login_company_id cookie anywhere.

Why would this not be set?

+1  A: 

Simple answer; there's a bug in django-debug-toolbar:

http://github.com/robhudson/django-debug-toolbar/issues/#issue/6

When intercept_redirects is disabled, this all works.

Chris R