为什么我的 cookie 没有被设置?
我正在编写一个 django 登录视图,它重复使用通用登录视图来完成大部分繁重的工作,但之后我会处理一些细节:
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
抛开对在自定义表单中设置默认公司的机制的任何评论,我'我想知道为什么我的 COMPANY_COOKIE
没有被设置。
我使用 Django 调试工具栏并将INTERCEPT_REDIRECTS
设置为True
,并且我可以看到我的日志语句正在被调用。如果我在 set_cookie 之后插入 pdb.set_trace(),我可以看到 response.cookies 包含我的公司 ID cookie。然而我的浏览器没有这个功能。在 DjDT 中,我可以看到我的请求的 COOKIES 变量不包含我的 cookie,并且在 chrome 的存储检查器中我看到了同样的事情;任何地方都没有 last_login_company_id
cookie。
为什么不设置这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单回答; django-debug-toolbar 中有一个错误:
http://github .com/robhudson/django-debug-toolbar/issues/#issue/6
当拦截重定向被禁用时,这一切都有效。
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.