How can I build multiple submit buttons django form

+1 vote

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?

Jun 26, 2020 in Python by kartik
• 37,520 points
2,801 views

1 answer to this question.

0 votes

Hiii @kartik,

You can use self.data in the clean_email method to access the POST data before validation. It should contain a key called newsletter_sub or newsletter_unsub depending on which button was pressed.

# in the context of a django.forms form

def clean(self):
    if 'newsletter_sub' in self.data:
        # do subscribe
    elif 'newsletter_unsub' in self.data:
        # do unsubscribe

Hope it work!!
Thank You!

answered Jun 26, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

How can I build a recursive function in python?

I'm wondering whether you meant "recursive". Here ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,160 views
0 votes
2 answers

How can I rename multiple files in a certain directory using Python?

import os from optparse import OptionParser, Option class MyOption ...READ MORE

answered Jul 29, 2020 in Python by The real slim shady
4,757 views
0 votes
1 answer

How can I have only positive decimal numbers in Django using Python?

Hi, are you aware of something called ...READ MORE

answered Jan 30, 2019 in Python by Nymeria
• 3,560 points
6,352 views
0 votes
1 answer

How can I prevent brute force login attacks using Django in Python?

Hi. Django-axes is an already existing application ...READ MORE

answered Feb 15, 2019 in Python by Nymeria
• 3,560 points
2,439 views
0 votes
1 answer

Error:Python3.4 can't install mysql-python

Hello @kartik, You can resolved this by the ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
2,173 views
0 votes
1 answer

What is `related_name` used for in Django?

Hello @kartik, The related_name attribute specifies the name of the ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,840 points
10,885 views
0 votes
1 answer

Django Server Error: port is already in use

Hello @kartik, Just type: sudo fuser -k 8000/tcp. This ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,840 points
1,790 views
0 votes
1 answer

How can I have Multiple Models in a single django ModelForm?

Hello @kartik, You can just show both forms ...READ MORE

answered Aug 13, 2020 in Python by Niroj
• 82,840 points
11,944 views
0 votes
1 answer

How can I get the domain name of my site within a Django template?

Hello kartik, The variation of the context processor ...READ MORE

answered Apr 23, 2020 in Python by Niroj
• 82,840 points
9,858 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP