I want to implement two submit type buttons in the Django template
I have a form that shows me the objects created in the database
class CampanaElectoralForm(forms.Form):
campana_electoral = forms.ModelChoiceField(required=False, queryset=CampanaElectoral.objects.all(), label="Campaña electoral")
def __init__(self, user=None, *args, **kwargs):
super(CampanaElectoralForm, self).__init__(*args, **kwargs)
self.fields['campana_electoral'].widget.attrs.update({'class': 'select2'})
I already have a button that goes to a view, which filters according to the selected option of the form, this button is currently working.
Now I need to add a second button that calls another view and I send it as a parameter the value of the selected option in the form
I'm currently doing it this way, but it doesn't work
<form action="" method="post" enctype="multipart/form-data">
{% load crispy_forms_tags %}
{% crispy form %}
<input type="submit" value="Filtrar" class="btn btn-success"
onclick=this.form.action="{% url 'estadisticas/' %}"/>
<input type="submit" value="Informe Ejecutivo" class="btn btn-success"
onclick=this.form.action="{% url 'reportes/informe_ejecutivo/' %}"/>
</form>
In a form there cannot be two
submit
, the trick for this is to change the typesubmit
tobutton
, and with a JavaScript function pass the url change the action and do thesubmit
What was written became code!
It's just a matter of declaring it in a different href for each input.