4

I wrote an simple django app to store e-mail adresses if the user fills in a form. Everything worked fine but when i followed this tutorial to integrate my app in django-cms i noticed that my form is not displaying in the Plugin as it did before i moved it to django-cms. It seems that {% csrf_token %} and {{ form.as_p }} are not working in the Plugin. Anyone knows why?

Here my cms_plugin.py

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from newsletter_djangocms.models import NewsletterPlugin
from django.utils.translation import ugettext as _


class CMSNewsletterPlugin(CMSPluginBase):
    model = NewsletterPlugin 
    module = _("Recipients")
    name = _("Newsletter Plugin")  
    render_template = "newsletter_djangocms/add.html"  
    def render(self, context, instance, placeholder):
        context.update({'instance': instance})
        return context

plugin_pool.register_plugin(CMSNewsletterPlugin) 

models.py of main app (not CMSPlugin)

from django.db import models

class Recipient(models.Model):
    adress = models.EmailField(max_length=100)
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)
    def __str__(self):
            return self.adress

and forms.py (also main app)

from django import forms
from .models import Recipient

class NewsletterForm(forms.ModelForm):

    class Meta:
        model = Recipient
        fields = ('adress','name', 'lastname',)

In which file could be the mistake or where could i find a helpfull post or something. Only thing i found here is this. But that not worked for me. I would be very glad about a good tip.

Community
  • 1
  • 1
Lepus
  • 567
  • 6
  • 22
  • 1
    Any chance you could describe what DOES happen when you display your plugin? Also, some other questions, have you created any plugins that do work? Which version of Python/Django/django CMS? Can you share your template markup? One thing that trips up new CMSPlugin users when handling forms, you need to understand that a form in a plugin generally can't POST to itself, because it doesn't have a unique URL and, a single plugin may be present many times on the same page, so, POSTing normally is complicated. However, posting using AJAX works well as noted in the links that @m15hbah posted below. – mkoistinen Feb 19 '15 at 22:42
  • well {% csrf_token %}{{ form.as_p }} of my form not displaying just the submit button. Yes i braught 2 other applictions to work with django-cms before, but there the forms were no problem because they just were in django-admin. Now i need them to be available for visitors of my site. I used django 1.7, python2.7, and django-cms 3.0. Atm i'm following m15hbah's links but will need a bit. thanks for answer – Lepus Feb 20 '15 at 00:29
  • Lepus: did you find out, how to get a form `{{ form.as_p }}` in a template to render properly? The render context needs to provide some information for this I guess. The answer of mishbah rather points to djangocms-forms. It is not for writing a plugin for third party applications as I hopefully understand. – woodz Feb 15 '19 at 18:42

1 Answers1

4

Really good example on how to integrate a form into a plugin:

https://github.com/mkoistinen/staff-app-demo/tree/master/contacts

A really good video tutorial here:

http://youtu.be/XIdClQgYdzw

mishbah
  • 5,487
  • 5
  • 25
  • 35