How can I build multiple submit buttons django form?
Eg:
if 'newsletter_sub' in request.POST: # do subscribeelif 'newsletter_unsub' in request.POST: # do unsubscribe
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 formdef clean(self): if 'newsletter_sub' in self.data: # do subscribe elif 'newsletter_unsub' in self.data: # do unsubscribe
You can also do like this,
<form method='POST'> {{form1.as_p}} <button type="submit" name="btnform1">Save Changes</button> </form> <form method='POST'> {{form2.as_p}} <button type="submit" name="btnform2">Save Changes</button> </form>
CODE
if request.method=='POST' and 'btnform1' in request.POST: do something...if request.method=='POST' and 'btnform2' in request.POST: do something...