Páginas

quinta-feira, março 10, 2016

Django Crispy Forms without form tag

I'm experiencing some problems with buttons and crispy forms with translations in Django. It appears that the render is caching the button rendering, and when the user changes the language buttons still use the first language used by users.

To solve this I removed buttons from the form layout and put it on the HTML template. However by default Crispy open and close the form tag. To solve this, just insert in the __init__ of your form:

self.helper.form_tag = False

It will set in Crispy that it does not have to open and close the form tag, and you can put buttons as you need.

Check Crispy's documentation at:
http://django-crispy-forms.readthedocs.org/en/latest/form_helper.html

_______________________________________________________________

Estou tendo alguns problemas com o Django Crispy forms e botões com internacionalização. Parece que ele cacheia a renderização dos botões e quando mudo o idioma ele não muda o conteúdo dos botões. Estranhamente somente dos botões.

Pra resolver o problema eu retirei os botões do layout dos forms no crispy e fiz inserção no html, o problema é que por padrão o Crispy abre e fecha a tag form. Pra que ele não faça isso coloquei no  __init__ do meu form:

self.helper.form_tag = False

Isso diz para o Crispy que ele não deve abrir e fechar a tag e ele renderiza somente os campos.

Confira a documentação do Crispy em:
http://django-crispy-forms.readthedocs.org/en/latest/form_helper.html

________________________________________________________________

Code exemple:
from crispy_forms.helper import FormHelper
from .form_layouts import MY_FORM_LAYOUT

class MyForm(forms.Form):
    some_field = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = MY_FORM_LAYOUT
        self.helper.form_tag = False