【发布时间】:2017-02-23 17:48:30
【问题描述】:
在 Django 中,我尝试使用以下代码同时渲染模板和发送 cookie:
template = loader.get_template('list.html')
context = {'documents': documents, 'form': form}
if ('user') not in request.COOKIES:
id_user = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(30))
HttpResponse.set_cookie(key='user', value=id_user, max_age=63072000)
return HttpResponse(template.render(context, request))
但我得到了错误:
/myapp/list/ 处的类型错误
set_cookie() 缺少 1 个必需的位置参数:'self'
我已经检查了documentation,但我没有找到解决方案。 请帮帮我:)
【问题讨论】:
-
这一行中:
HttpResponse.set_cookie(key='user', value=id_user, max_age=63072000)HttpResponse 是类还是类(对象)的实例?
标签: python django httpcookie