Django cookies place double quotes around email address

坚强是说给别人听的谎言 提交于 2020-01-04 06:35:55

问题


On my login script it creates a cookie for the user logging in of their email address and password. Problem I am having is when the email address is set it puts the entire email address between double quotes. How would I get it to not?

if request.method == 'POST':
     post = request.POST
     email = post.get('email', None)
     response.set_cookie('emailaddress', email, max_age=expire_v)

回答1:


You can try strip method

email.strip('"')



回答2:


Another solution for this issue is to directly work with a SimpleCookie object and attach it to your response

>>> from Cookie import SimpleCookie
>>> mycookie = SimpleCookie()
>>> mycookie['emailaddress'] = 'josuebrunel@gmail.com'
>>> mycookie['emailaddress']['expires'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
>>> print(mycookie)
Set-Cookie: emailaddress="josuebrunel@gmail.com"; expires=2015-11-25 22:20:16
>>> response.cookies = mycookies

I had the same issue, and i fixed by using SimpleCookie



来源:https://stackoverflow.com/questions/7992603/django-cookies-place-double-quotes-around-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!