Alex Rivera | Logout

how to use forms in django-cms?

Asked 2011-03-26T15:04:57.047
16

I am an absolute novice to django-cms. I have gone through the tutorial and configured it exactly the same as mentioned in the documentation. Now, I have to build an application which uses a form to upload products.

I dont have a clue as to how to move ahead with it. I want to start it with simple forms as if now, say, a username and password textbox kind of. How can I use django forms in the django-cms page? I have snippet plugin enabled in it too. I need some guidance for this.

Any suggestions plsss. thanks

Edit
Report

1 Answer

6

Assuming your product application works as expected without Django CMS, what you'd next want to do is create your own plugin to show the form. The plugin would render your form, which you've already defined in your own application using a plugin template that you've created.

This plugin for a contact form allows the contact form to be inserted into the page template anywhere a placeholder will allow it.

class ContactPlugin(CMSPluginBase):
    """Enables latest event to be rendered in CMS"""

    model = CMSPlugin
    name = "Form: Contact"
    render_template = "contact_form/contact_plugin.html"

    def render(self, context, instance, placeholder):
        request = context['request']
        context.update({
            'instance': instance,
            'placeholder': placeholder,
            'form': ContactForm(request=request),
        })
        return context

The template would include all the HTML and Django template language necessary to render the form.

This other contact form plugin shows another example of how to do it. Instead of rendering the form it just updates the context. The upside is that you don't have to create a separate template, but the downside is that you lose some of the modularity of having a plugin. This depends on the page template rendering the form.

class ContactPlugin(CMSPluginBase):
    model = Contact
    name = _("Contact Form")
    render_template = "contact.html"

    def render(self, context, instance, placeholder):
        request = context['request']

         if request.method == "POST":
            form = ContactForm(request.POST)
            if form.is_valid():
                form.send(instance.site_email)
                context.update( {
                    'contact': instance,
                    })
            return context
        else:
            form = ContactForm()

            context.update({
            'contact': instance,
      
answered 2011-03-26T16:20:16.597

Your Answer