The use of two cartilaginous forms of django

I have difficulty using two cartilaginous forms of django at the same time. I have one form to simply enter new data into my application and another form that appears in the bootstrap module for users to provide feedback. Below I have stripped my template to bare basics.

I have a group form:

class Crispy_Group_Form(forms.ModelForm): def __init__(self, *args, **kwargs): self.helper = FormHelper() # self.helper.form_tag = False self.helper.form_class = 'form-horizontal' self.helper.layout = Layout( Fieldset( 'New Group', Field('name', placeholder='Group Name'), Field('notes', placeholder='Group Notes', rows='10', css_class='input-xxlarge'), ), FormActions( Submit('save_changes', 'Save changes', css_class="btn-primary"), HTML(' | '), Submit('cancel', 'Cancel'), ) ) self.helper.form_id = 'id-Crispy_Group_Form' self.helper.form_method = 'post' super(Crispy_Group_Form, self).__init__(*args, **kwargs) class Meta: model = Group exclude = ['slug'] 

and contact form

 class Crispy_ContactForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.helper = FormHelper() self.helper.form_class = 'form ajax' self.helper.form_action = 'feedback' self.helper.form_tag = False self.helper.layout = Layout( Fieldset( 'Contact Form', Field('topic', placeholder='Topic', css_class='input-medium'), Field('subject', placeholder='Subject', css_class='input-xlarge'), Field('message', placeholder='Message', rows='5', css_class='input-xlarge'), Field('sender', placeholder='Sender', css_class='input-xlarge'), ), ) self.helper.form_id = 'id-Crispy_ContactForm' self.helper.form_method = 'post' super(Crispy_ContactForm, self).__init__(*args, **kwargs) class Meta: model = Feedback exclude = ['creation_date'] 

My opinion:

 def bootstrap_test(request): return render_to_response( "bootstrap_test.html", { 'feedback_form' : Crispy_ContactForm, 'form' : Crispy_Group_Form, }, context_instance=RequestContext(request)) 

And my simplest template:

 {% load crispy_forms_tags %} <form action="{% url feedback %}" method="post" id="id-Crispy_ContactForm" class="form"> {% crispy feedback_form %} </form> {% crispy form %} 

The feedback form is displayed twice. As if both forms have the same shape. If I remove the feedback form from the template, it will display the group form. If I regroup them so that {% crispy form%} is higher than reverse_selection, it displays two different forms correctly.

I read the documentation but could not find a way that works.

Why is this happening and what do I need to configure so that it displays correctly?

+4
source share
1 answer

OK I understood. Crispy forms seem to overwrite the "shape" variable. This is why it returns the same two forms:

 <form action="{% url feedback %}" method="post" id="id-Crispy_ContactForm" class="form"> {% crispy feedback_form %} </form> {% crispy form %} 

but this returns two different forms:

 {% crispy form %} <form action="{% url feedback %}" method="post" id="id-Crispy_ContactForm" class="form"> {% crispy feedback_form %} </form> 

The first example overwrites the "form" variable before the second call to the form can capture it.

I just wanted to confirm that this is not what I did wrong. So I just changed the order in which the two forms are called, so that the instance {% crispy form%} first appears. I could not use another form for the form because I use the create / update_object functions.

+2
source

Source: https://habr.com/ru/post/1440799/