Search code examples
pythondjangodjango-cmsdjango-sessions

Django/Django-CMS Session variables with old values


I've got a site in Django/Django-CMS in which I want to save some data from one page to another. I'm saving the data in the session variable:

request.session['yb_name'] = request.POST.get('name')

The problem is that sometimes my pages get and old value of yb_name instead of the new one. I print the variable in my context processor and the value is the right one but in the template shows me and old one. This doesn't happen every time. Also this happens inside templates from custom plugins I've made.

I print it in the template like this:

<input type="text" name="name" value="{{ request.session.yb_name|default_if_none:'' }}">

The first thing that I tried was to delete the variable and then create it again the the new value:

if request.session.get('yb_name', None):
    del request.session['yb_name']
request.session.modified = True
request.session['yb_name'] = request.POST.get('name')
request.session.modified = True

But the problem persists.

Any ideia what i could be?

Thanks :)


Solution

  • As suggested by @Paulo I turned off the CMS cache. In my settings.py file I added this lines:

    CMS_PAGE_CACHE = False
    CMS_PLACEHOLDER_CACHE = False 
    CMS_PLUGIN_CACHE = False
    

    This disables all cache but as suggested by @brunodesthuilliers it could be bad in production so I searched a little in the Django-CMS documentation and found a setting you can put to disable just some plugins:

    class HistoryHeaderCMSPlugin(CMSPluginBase):
        render_template = "plugins/history/header.html"
        name = _("History Header")
        model = HistoryHeaderPlugin
        cache = False
    
        def render(self, context, instance, placeholder):
            context = super(HistoryHeaderCMSPlugin, self).render(context, instance, placeholder)
            return context
    

    The cache = False in the plugins that used my sessions variables solved my problem without losing all the CMS cache.

    Thank you all :)