0

In my Django application, I am trying to use sessions to store a variable in my class view and later use it to send it to a function view.

In the past I have used sessions on function views and now I am trying to use it on a class view.

class someView(CreateView):
    form_class = myForm
    template_name = 'myTemplate.html'
    ...
    ...

    def get_context_data(self, **kwargs):
        request.session['my_var'] = '1234'
        return context

Than I am trying to capture the session variable in another view like this:

def fnView(request):
    data = {}
    new_var = request.session['my_var']
    data = {'key1': new_var}
    return render(request, 'fnVwTemplt.html', data)

And in the template I am trying to display the variable like this:

{{ key1 }}

However I am coming up with the following message in the browser:

KeyError at ...

'my_var'

It is evident that the variable is not getting stored in the session variable.

What I am doing wrong?

PS. Apologies, if my query is not lucid enough.

shaan
  • 351
  • 2
  • 15
  • `It is evident that the variable is not getting stored in the session variable.` had you checked session storage in the browser from someView `myTemplate.html` ? to make sure that your variable actually saved in the session – Linh Nguyen Jul 29 '20 at 04:42
  • if you want to avoid error from a key better to use `request.session.get('my_var', None)` – Linh Nguyen Jul 29 '20 at 04:49
  • @LinhNguyen - Thanks so much for your reply. I tried to use `request.session.get('my_var, None)`, however getting `None` in my template. And, where exactly do I check in the browser for **session storage**? – shaan Jul 29 '20 at 06:48
  • Yeah, got it. It should be in `Developr Tools > Application > Storage > Session Storage > Site url`. No, there is **no entry there**. So it seems that the variable is not getting stored at all. – shaan Jul 29 '20 at 06:56
  • so the problem is from get_context_data, try changing it to use `self.request.session['my_var'] = '1234'` instead – Linh Nguyen Jul 29 '20 at 07:15
  • Still getting `None`. On a hunch I thought of checking if the app is hitting the view `someView` at all. I changed the `get_context_data` code (misspelt `self` to `selfx`). And still there is no `Error`. Neither in the terminal on module `save` nor when calling the view. Something very wrong here... – shaan Jul 29 '20 at 07:25
  • well i can't see any problem as the method to set session variable is correct [docs](https://docs.djangoproject.com/en/3.0/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase.__setitem__), did your `get_context_data` to run in the template view ? – Linh Nguyen Jul 29 '20 at 07:29
  • Could you please explain *did your get_context_data to run in the template view ?* If I am getting it correct, I am using it on a `CreateView` (as shown in my original post) alongwith **PermissionRequiredMixin** (to check for user permission on the model). – shaan Jul 29 '20 at 07:35
  • sorry i'm not familiar with using generic view but your way of using is the same as this [question](https://stackoverflow.com/a/21904529/11225821) maybe you can look into it – Linh Nguyen Jul 29 '20 at 07:54
  • @LinhNguyen - Thanks for taking time out for me. I will check the one suggested by you. – shaan Jul 29 '20 at 08:12
  • 1
    i think you need to override the context first for get_context_data to work. Add `context = super(someView, self).get_context_data(**kwargs)` in `get_context_data` before return – Linh Nguyen Jul 29 '20 at 08:13
  • @LinhNguyen - I am sorry but I just realized that I already have `context = super().get_context_data(**kwargs)` which I failed to reproduce in my question. I have now changed it to `context = super(someView, self).get_context_data(**kwargs)`, as suggested by you. Unfortunately still not getting the session data. – shaan Jul 29 '20 at 08:27

0 Answers0