DjangoCMS Plugins and POST Requests
I use DjangoCMS at work some times. I have a love-hate relationship with it. OK, I mostly hate it, but it’s not its fault (usually).
Sometimes clients need to be able to configure stuff. Maybe you have a form that you need to ship over to Pardot, so you can run a drip campaign. Whatever the use-case is, you need to allow users to configure stuff themselves.
I figured out from cmsplugin_contact_plus that in a DjangoCMS plugin you can do all this madness:
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from .forms import ThingForm
from .models import ThingPluginConfig
model = ThingPluginConfig
name = 'Thing Form'
render_template = 'things/plugin_base.html'
allow_children = False
def render(self, context, instance, placeholder):
request = context['request']
form = ThingForm(request.POST) if request.method == 'POST'\
obj = form.save()
# Do other stuff
form = ThingForm() # wipe form
In short, the render method has access to the context dict, and context has, of course, the request. And if the request is a POST, then you can run the POST through a form. TADA!
It’s some real crazy crap for sure. But if you need to do it, you indeed can.
EDIT: /u/ojii over at reddit has pointed out that if you use multiples of these on a single page in Django CMS, that you’ll have very weird side effects.
He’s totally right. This isn’t a one size fits all pattern.