/cms-form-plugin

Django CMS Form plugin inspired by django.views.generic.edit.FormView

Primary LanguagePythonGNU General Public License v2.0GPL-2.0

cms-form-plugin

Django CMS Form plugin inspired by django.views.generic.edit.FormView

Installation

  • This project is in Beta, clone it directly from github or execute: pip install -e git+https://github.com/metzlar/cms-form-plugin#egg=cms-form-plugin

  • Add cms_form_plugin to INSTALLED_APPS

  • Specify FORM_CLASSES in settings.py a tuple of tuples to be used as choices attribute to the plugin's form_class field. For example:

    FORM_CLASSES = ( ( 'django.contrib.auth.forms.AuthenticationForm', 'Login Form' ), )

  • Specify FORM_TEMPLATES in settings.py a tuple of tuples to be used as choices attribute to the plugin's render_template field. For example:

    FORM_TEMPLATES = ( ( 'form/form.html', 'Default Form Template' ), )

  • Include cms_form_plugin.urls in your urlpatterns

  • Run manage.py migrate cms_form_plugin

Usage

Add CMS Form Plugin to any placeholder. It requires form_class to be configured per plugin instance. form_class points to the full path of the Django Form subclass you want to include.

Consider the following form in myapp.forms :

class MyForm(forms.Form):
    name = forms.CharField()
    category = forms.CharField()

    def clean(self):
        data = super(MyForm, self).clean()

        # check validity of data['name']
        # do something with data['..']

        if not_valid:
            raise forms.ValidationError('Invalid')

Now add MyForm to settings.FORM_CLASSES and start adding it to tthose placeholders.

CMS Form Plugin to a placeholder with form_class set to myapp.forms.MyForm More configuration

form_class - full path to the class to use as form. Must be an entry from settings.FORM_CLASSES to make the plugin easier to use by civilians (non-developers).

success_url - url to redirect to when the form got successfully validated.

success_page - page to redirect to when no success_url is specified and the form got successfully validated. If both success_url and success_page are undefined, the redirect uses the current page.

submit_caption - caption for the submit button. Defaults to 'Submit'

The form_class can define get_success_url(request) to be more flexible in creation of the success url. Consider the following example:

class MyForm(forms.Form):

    def clean(self):
        super(MyForm, self).clean()
        self.m = MyModel(path = self.request.path)
        self.m.save()

    def get_success_url(self):
        return self.m.get_absolute_url() or self.m.path