Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Session key in Horizon disappear

I have a strange behavior with sessions engine. In my form handle (in a file forms.py) I save value in request.session

class RLForm(forms.SelfHandlingForm):
    datacenter_id = forms.CharField(widget=forms.HiddenInput())
    username = forms.CharField(label=_("User Name"),
                           widget=forms.TextInput(attrs={"autofocus": "autofocus"}),
                           error_messages={
                           'required': _('This field is required.')})

    def clean(self):
        data = self.cleaned_data
        return data

    def handle(self, request, data):
        try:
            [...]
            request.session['datacenter_id'] = datacenter_id
            print(request.session.session_key)
            #datacenter_id is stored in my sessions
            return projects
        exept Exception:
            [...]

In views.py and my class, I have a get_success_url:

class RLView(forms.ModalFormView):
    form_class = RLForm
    template_name = 'project/rdc/rl.html'

    def get_success_url(self):
        print(self.request.session.keys())
        #still have my datacenter_id key in my session
        url = "/project/rdc/" + self.kwargs['datacenter_id'] + "/list-r-nets"
        return url

The destination is a other class based views

class NetView(tables.DataTableView):
table_class = NetDCTable
template_name = 'project/rdc/net.html'


def get_data(self):
    (print(self.request.session.keys())
    # datacenter_id not exist anymore !!!
    datacenter_id = self.request.session['datacenter_id']

This error is returned :

Exception Type:     KeyError
Exception Value:    'datacenter_id'

This is my settings :

SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
SESSION_SAVE_EVERY_REQUEST = True 
SESSION_COOKIE_HTTPONLY = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_SECURE = False
SESSION_TIMEOUT = 1800

Do I miss something? Why my session key disappear? I use Horizon from github.