0

I'm writing a module to store comments for specific article. My model contains a column unique_session which is intended to store the commenter's browsing session id.

enter image description here

Within my view function, I'm trying to initialize a variable bearing the value of request.COOKIES['sessionid'] which I later assign to unique_session column of the model.

enter image description here

So far, I'm doing good with Chrome. I'm able to get it as the key exists when I inspect it, and obviously it stores to db just fine.

enter image description here

But when I go to Firefox, the sessionid cookie does not exist and Django throws in a KeyError.

enter image description here

enter image description here

Anyone have any idea what could be missing?

P.S. I was not manually creating the sessionid cookie. I saw it exists in Chrome so I just used it instead of creating my own 32-bit session key. Then I later realized that it does not exist on Firefox. So I have no idea when/where it gets created and why not on Firefox.

6
  • Login any account in firefox, try and report Aug 23, 2019 at 8:48
  • Don't do any of this. Use the actual Django session functionality. Aug 23, 2019 at 9:10
  • @DanielRoseman isn't request.COOKIES['sessionid'] a native Django session functionality? I never created this cookie but it exists on Chrome. Also I've read that Django set the default value of DJANGO_COOKIE_NAME's as 'sessionid'
    – Michael
    Aug 23, 2019 at 9:18
  • 1
    request.session.session_key is what @DanielRoseman intends. By explicitly using the cookie you're making assumptions on how the session id is passed around.
    – dirkgroten
    Aug 23, 2019 at 9:40
  • 2
    is the user logged in? Note django only creates a session (and sends the session id to the browser) when it's really necessary, i.e. either when the user logs in or when you actually set some key/value in the session. Open a new private window in Chrome, fetch your site and you'll see: no session id cookie.
    – dirkgroten
    Aug 23, 2019 at 10:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.