0

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 :)

patricia
  • 1,075
  • 1
  • 16
  • 44

1 Answers1

0

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 :)

patricia
  • 1,075
  • 1
  • 16
  • 44
  • This may _seem_ to solve your problem right now, but this will yield another problem as soon as you deploy on production and start getting some serious traffic. There's a reason why django-cms uses caching... – bruno desthuilliers Jan 13 '17 at 12:27
  • @brunodesthuilliers do you have other solution? – patricia Jan 13 '17 at 12:29
  • @brunodesthuilliers and I'm using `POST` because its from a form submit. – patricia Jan 13 '17 at 12:29
  • Django-CMS doc on caches is quite scarce, and we don't know in which part of the code and templates this session variable is set / used, so there's no way I could give you a working solution here obviously. If this is only set / used by a custom plugin you could disable this plugin's cache only, but I'm not sure the page cache won't still fire. IOW: you will have to read the source to understand how Django-CMS uses thoses caches and take action accordingly. Sorry but no free lunch today ;) – bruno desthuilliers Jan 13 '17 at 12:39