I am trying to create a crisp form with the degego on which there is a constant form on the top, but then a variable The form is at the bottom of the same page, which depends on the option from the drop-down menu.
Documents of crispy forms say that this is possible when doing the following:
& lt; Form action = "{% url submit_survey%}" class = "uniform" method = "post" & gt; {% Crispy first_form%} {% crispy second_form%} & lt; / Form & gt;
But this is obviously dependent on the reference names of the forms, but I can not find any resources that will actually tell the set variables of those forms which I Want to do
Below is an example of the top form and possibly the following form: crispy_forms.layout import layout, buttonholder, from django import forms from form.Helper from crispy_forms.helper import: Submit square BusinessForm (forms.ModelForm): group_id = forms.ModelChoiceField (GroupModel.objects.all ()) def __init __ (self, * args, ** kwargs): Super (BusinessForm, self) .__ init __ (* args, ** kwargs) self.helper = FormHelper (self) self.helper.form_tag = false self.helper.layout = layout ( 'group_id', 'CONTACT_NAME', 'BUSINESS_NAME', 'address_line_1', 'address_line_2', ' Postcode ',' telephone ',' email ',' web ', buttonholder (class meta: model = bus) InessModel fields = Submit ('Submit', 'Add Business', css_class =' btn-primary ')) Submit' group_id ',' contact_name ',' business_name ',' address_line_1 ',' address_line_2 ')) Submit' postcode "," Telephone "," e-mail "," web ") square AttractionExtrasForm (forms.ModelForm): classification_id = forms.ModelChoiceField (AttractionClassModel.objects.all ()) def __init __ (self, * args, * Kwagrs *): Super (AttractionExtrasForm, self) .__ init __ (* args, ** kwargs) self.helper = FormHelper (self) self.helper.form_tag = false self.helper.layout = layout ( 'classification_id',) Class meta model = attraction extroSodel field = ('classification_id',)
More views:
class BusinessView (views.log inRequiredMixin, views.FormValidMessageMixin, generic.edit.CreateView): form_class = BusinessForm model = business model TEMPLATE_NAME = "businesses / business.html" success_url = reverse_lazy ( 'businesses: add_business') From_valid_message = "You have successfully created a business." Def form_valid (auto, form): Returns Super (Business V, Self) .form_valid (form)
and template:
{% 'extends' Add _layouts /base.html '%} {% load crispy_forms_tags%} {% block title%} business. {{Block.super}} {% endblock title%} {% block title%} & lt; H1 & gt; Add business & lt; / H1> {% Endblock headline%} {% block content%} & lt; Form action = "{% url add_business%}" method = "post" & gt; {% Crispy form%} & lt; / Form & gt; {% Endblock content%}
This will display the normal form, because it is only calling form variables.
Does anyone know this?
Cheers
view :
class BusinessView (...): ... def get_context_data (self, ** kwargs): reference = Super (BusinessView, self) .get_context_data (** kwargs) reference [ 'form_2'] = AttractionExtrasForm (Example = self .model ()) # if createview # context ['form_2'] = attraction extro-form (example = self.object) # if update return return reference
To validate from
if form.is_valid () and form_2.is_valid (): return self.form_valid (form, form_2) Other: Returns autform_invalid (FO (Form, form_2)
If you have connected models (each other has the FK field)
def form_valid (self, form, form_2): if No self.object: # createview Self.object = form.save () Other: # updateview form.save () form_2.instance = self.object form_2.save () returns HttpResponseRedirect (self.get_success_url ()) def form_invalid (auto , Form, form_2): Return Self.render_to_response (self.get_context_data (form = form, form_2 = form_2))
And if you want that many examples of form_2 < / Code> can use inlineformat_anyetry
Template :
& lt; Form method = "post" & gt; {% Csrf_token%} {% crispy form%} {% crispy form_2%} & lt; / Form & gt;
Comments
Post a Comment