I have form with one input for email and two submit buttons to subscribe and unsubscribe from newsletter:
<form action="" method="post">
{{ form_newsletter }}
<input type="submit" name="newsletter_sub" value="Subscribe" />
<input type="submit" name="newsletter_unsub" value="Unsubscribe" />
</form>
I have also class form:
class NewsletterForm(forms.ModelForm):
class Meta:
model = Newsletter
fields = ('email',)
I must write my own clean_email method and I need to know by which button was form submited. But the value of submit buttons aren't in self.cleaned_data
dictionary.
Could I get values of buttons otherwise?
one url to the same view! like so!
It's an old question now, nevertheless I had the same issue and found a solution that works for me: I wrote MultiRedirectMixin.
You can use
self.data
in theclean_email
method to access the POST data before validation. It should contain a key callednewsletter_sub
ornewsletter_unsub
depending on which button was pressed.Eg:
You can also do like this,
CODE