django-unicorn icon indicating copy to clipboard operation
django-unicorn copied to clipboard

unicorn does not work with form helpers e.g. crispy

Open krystofbe opened this issue 2 years ago • 2 comments

just found out about this awesome library. thanks a lot. love it! <3

while playing around with the unicorn, I noticed that i can't use the form validation with form_class when using form helpers or crispy forms and get some error like this

django.template.base.VariableDoesNotExist: Failed lookup for key [form]  ...

this is probably related to https://github.com/adamghill/django-unicorn/issues/324.

has anybody found a way to make the unicorn validation functionality work together with crispy forms or form helpers in general?

krystofbe avatar Feb 16 '22 23:02 krystofbe

Thanks for the nice words! I think @nerdoc mentioned some investigation with crispy forms and Unicorn in https://github.com/adamghill/django-unicorn/discussions/227 which might be helpful? However, if you could make a PR with some sample code using crispy forms I could see if I could add anything to unicorn to help in future releases.

adamghill avatar Feb 17 '22 02:02 adamghill

Bad Hack(TM): patch Unicorn, to let UnicornView inherit from FormView instead of TemplateView.

class MyForm(Form)
    email = forms.EmailField()

# Unicorn Component
class EmailView(UnicornView):
    form = MyForm

And in the template:

{% load unicorn crispy_forms_tags %}
<div>
{% crispy form %}
</div>

I just wrote this here in a few minutes, so it's not tested and copy'n'pasted, but it should work,

Whether it is a good idea to "patch" Unicorn and urge it to be a FormView - I don't know - that has to decide @adamghill - there is another discussion for that...

Cheers

nerdoc avatar Feb 28 '22 11:02 nerdoc