Skip to content

Instantly share code, notes, and snippets.

@danjac
Last active July 5, 2017 19:20
Show Gist options
  • Save danjac/10e9e21d3da34324a779b8a5cc50caf2 to your computer and use it in GitHub Desktop.
Save danjac/10e9e21d3da34324a779b8a5cc50caf2 to your computer and use it in GitHub Desktop.
from .layout import FormHelper, FormHelperMixin
class MyForm(FormHelperMixin, forms.ModelForm):
class FormHelperMeta:
# works like Meta, we just use to store property overrides
# doesn't need to inherit
# helper_class = MyCustomHelper
submit_text = "Submit!"
action = reverse('account:login')
class Meta:
model = MyModel
# layouts.py
class EmptyHelperMeta:
pass
class FormHelperMixin(object):
def __init__(self, *args, **kwargs):
self.helper = self.get_form_helper()
def get_form_helper(self):
meta = getattr(self, 'FormHelperMeta', EmptyHelperMeta)
helper_cls = getattr(meta, "form_helper", FormHelper)
helper = helper_cls()
helper.form_method = getattr(meta, "method", "POST")
# etc etc
return helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment