mkoistinen / cmsplugin-form-handler

This package provides a mechanism for handling form-submissions in django-CMS plugins.
MIT License
14 stars 10 forks source link

'MyPlugin' object has no attribute 'get_form_kwargs' #7

Closed brberis closed 6 years ago

brberis commented 6 years ago

Description:

Hi, I get this error when the form is submited:

Request Method: POST
Request URL:    http:/sitepath/plugin_forms/28/
Django Version: 1.11.12
Exception Type: AttributeError
Exception Value:    
'QuickContactPlugin' object has no attribute 'get_form_kwargs'
Exception Location: /home/projects/project/lib/python3.4/site-packages/cmsplugin_form_handler/views.py in get_form_kwargs, line 62

cms_plugins.py

@plugin_pool.register_plugin
class QuickContactPlugin(QuickContactPlugins):
    render_template = 'contact_form/quick_contact_plugin.html'
    name = _('Quick Contact Form')
    model = models.QuickContactPlugin
    form_class = ContactForm
    success_url = '/#sucess'

    def render(self, context, instance, placeholder):
        context = super(QuickContactPlugin, self).render(context, instance, placeholder)
        request = context.get('request')
        context['instance'] = instance
        context['form_info'] = instance.get_form_info(request)
        return context

    def get_form_class(self, request, instance):
        return self.form_class

    def get_success_url(self, request, instance):
        return self.success_url

models.py

from django import forms
from .models import Contact
from cmsplugin_form_handler.forms import FormPluginFormMixin

class ContactForm(FormPluginFormMixin, forms.ModelForm):

    class Meta:
        model = Contact
        fields = ('name', 'email', 'phone', 'message')

Any thoughts? and Thanks!

brberis commented 6 years ago

Sorry I put models.py but I meant forms.py

mkoistinen commented 6 years ago

What is 'QuickContactPlugins'?

brberis commented 6 years ago

Hello,

class TemplatePrefixMixin(object):
    def get_render_template(self, context, instance, placeholder):
        return self.render_template

class QuickContactPlugins(TemplatePrefixMixin, CMSPluginBase):
    module = 'RE Plugins'
brberis commented 6 years ago

in

models.py

....
....

class PluginEditModeMixin(object):
    def get_edit_mode(self, request):
        return (
            hasattr(request, 'toolbar') and request.toolbar and
            request.toolbar.edit_mode)

class ContactCMSPlugin(CMSPlugin):
    cmsplugin_ptr = models.OneToOneField(
        CMSPlugin, related_name='+', parent_link=True)

    class Meta:
        abstract = True

class QuickContactPlugin(PluginEditModeMixin,
                                   ContactCMSPlugin):

    quick_contact = models.ForeignKey(QuickContactForm, blank=True,
        help_text=_('Select form to show')
    )

    def get_form_info(self, request):

        queryset = QuickContactForm.objects
        queryset = QuickContactForm.objects.get(name = self.quick_contact)

        return queryset
mkoistinen commented 6 years ago

OK, I see nowhere in your class hierarchy where you include the FormPluginBase.

This can be imported from from cmsplugin_form_handler.cms_plugins import FormPluginBase

In your code above, change the base class for ContactCMSPlugin from CMSPlugin to FormPluginBase.

brberis commented 6 years ago

Oops.. Thanks. Now I'm stuck with this error:

Request Method: | POST
-- | --
/es/plugin_forms/50/
1.11.12
TypeError
argument of type 'NoneType' is not iterable
/home/projects/castorgc/lib/python3.4/site-packages/django/shortcuts.py in resolve_url, line 153
/home/projects/castorgc/bin/python3
3.4.3

Traceback:

File "/home/projects/castorgc/lib/python3.4/site-packages/django/shortcuts.py" in resolve_url
  147.         return reverse(to, args=args, kwargs=kwargs)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/urls/base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "/home/projects/castorgc/lib/python3.4/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  497.         raise NoReverseMatch(msg)

During handling of the above exception (Reverse for 'None' not found. 'None' is not a valid view function or pattern name.), another exception occurred:

File "/home/projects/castorgc/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/views/generic/edit.py" in post
  185.             return self.form_invalid(form)

File "/home/projects/castorgc/lib/python3.4/site-packages/cmsplugin_form_handler/views.py" in form_invalid
  112.         return redirect(url)

File "/home/projects/castorgc/lib/python3.4/site-packages/django/shortcuts.py" in redirect
  56.     return redirect_class(resolve_url(to, *args, **kwargs))

File "/home/projects/castorgc/lib/python3.4/site-packages/django/shortcuts.py" in resolve_url
  153.         if '/' not in to and '.' not in to:

Exception Type: TypeError at /es/plugin_forms/50/
Exception Value: argument of type 'NoneType' is not iterable
mkoistinen commented 6 years ago

Looks like you haven't defined a success URL. Have a look at the documentation, specifically here.