Saltar al contenido

El botón Enviar ya no funciona con los formularios crujientes de Django.

Si encuentras algún detalle que no comprendes puedes comentarlo y haremos todo lo necesario de ayudarte rápidamente.

Solución:

Como usted mismo incluye las etiquetas de formulario, el token csrf y el botón de envío en la plantilla, debe usar el filtro crujiente en lugar de la etiqueta crujiente.

% csrf_token % crispy

Si desea utilizar la etiqueta, puede definir su botón de envío dentro de un FormHelper. Consulte los documentos de etiquetas crujientes para obtener más información.

Para agregar el botón de enviar a su formulario, la documentación de crispy_form lo hizo de esta manera:

import floppyforms.__future__ as forms # you can use django's own ModelForm here
from crispy_forms.helper import FormHelper
from django.core.urlresolvers import reverse_lazy

class YourForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = 'post' # this line sets your form's method to post
        self.helper.form_action = reverse_lazy('your_post_url') # this line sets the form action
        self.helper.layout = Layout( # the order of the items in this layout is important
            'field1_of_your_model',  # field1 will appear first in HTML
            'field2_of_your_model',  # field2 will appear second in HTML
            # this is how to add the submit button to your form and since it is the last item in this tuple, it will be rendered last in the HTML
            Submit('submit', u'Submit', css_class='btn btn-success'), 
    )

    class Meta:
        model = YourModel

Luego, en su plantilla, todo lo que tiene que hacer es esto

% load crispy_forms_tags %
% crispy form %

Y eso es. No hay necesidad de escribir NINGÚN html en su plantilla.

Creo que el objetivo de crispy_forms es definir HTML en Python. Para que no tengas que escribir mucho HTML en tu plantilla.


Algunas notas adicionales:

Ya que estás usando bootstrap. Hay tres campos más que te serán útiles, dentro __init__() definido anteriormente, agregue estos si los necesita:

self.helper.form_class = 'form-horizontal' # if you want to have a horizontally layout form
self.helper.label_class = 'col-md-3' # this css class attribute will be added to all of the labels in your form. For instance, the "Username: " label will have 'col-md-3'
self.helper.field_class = 'col-md-9' # this css class attribute will be added to all of the input fields in your form. For isntance, the input text box for "Username" will have 'col-md-9'

Para otros que se quedan atascados aquí usando la etiqueta y queriendo agregar un html personalizado a su

Puedes agregar el self.helper.form_tag = False propiedad a su ayudante y no agregará un

hasta el final de su formulario.

Más documentos aquí.

Sección de Reseñas y Valoraciones

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *