0

I want to create a "Sign up for our newsletter" pop up that has a "No thanks" button.

I want Django to remember that the user clicked the "No thanks" button.

What I know is that sessions/cookies are set in their respective views. I see examples on StackOverflow of them set in the home/index view. What if the user visits a different page? Then the session variable won't be set unless they visit that one page.

I'd like for the same variable set regardless of what page they view.

TimeBigot
  • 47
  • 7
  • Possible duplicate of [how to setup custom middleware in django](https://stackoverflow.com/questions/18322262/how-to-setup-custom-middleware-in-django) – Robert Moskal Jun 17 '17 at 15:42

1 Answers1

2

Once a session variable is set, it is set across your whole app. If you have access to the request, you get it like this:

request.session['idempresa']

You set it once in a view (or in some middle ware) and it's available anywhere you have access to a request. That's how http sessions work.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Then would I have to set it for every view? I want it so that it doesn't matter which view they visit. How would I make it so that it's DRY? – TimeBigot Jun 17 '17 at 04:21
  • I get that it just takes one view to instantiate a session variable but I want it so that it doesn't matter which view the user visits. Will middleware instantiate the session variable from the get-go (aka from any view)? – TimeBigot Jun 17 '17 at 04:41
  • This seems to be a question about setting up and using django middleware. There are plenty of good posts about this. – Robert Moskal Jun 17 '17 at 15:41