How can I build multiple submit buttons django form?
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
Referencing multiple submit buttons in django
Give the input types a name and look for them in your request.POST
dictionary.
E.g.:
<form action="/" method="post">
{% csrf_token %}
<input type="text" name="{{ email.id }}" value=" {{email}}"></td>
<td><input type="submit" value="Edit" name="_edit"></td>
<td><input type="submit" value="Delete" name="_delete"></td>
</tr>
and in views.py something like
if request.POST:
if '_edit' in request.POST:
do_edit()
elif '_delete' in request.POST:
do_delete()
EDIT: Changed d.has_key(k)
to k in d
per Daniel's comment. has_key
is deprecated in python 3.0 and the in style is preferred as its more generic -- specifically d.has_key(k)
fails if d isn't a dictionary, but k in d
works for any d
that's an iterable (e.g., dict, string, tuple, list, set).
Is it possible to create two different submit button in one form in Django?
This is possible, in your view you would do something like this:
class SomeView(UpdateView):
...
def form_valid(self, form):
self.object = form.save(commit=False)
if 'add-email' in self.request.POST:
self.object.name = f'{self.object.name}@gmail.com'
self.object.save()
Then in your template:
<form ...>
<button type="submit" name="save">Submit</button>
<button type="submit" name="add-email">Submit with Email</button>
</form>
Django form with two submit buttons . . . one requires fields and one doesn't
The answer above works, but I liked this way better: Changing required field in form based on condition in views (Django)
I have two buttons:
<!-- simply saves the values - all fields aren't required unless the user is posting the venue -->
<input type="submit" name="mainForm" value="Save">
<!-- post the values and save them to the database - fields ARE required-->
<input type="submit" name="postVenue" value="Post Venue">
I make all form fields required=False
by default and then have this in my view:
if 'postVenue' in request.POST:
form = NewVenueForm(request.POST)
form.fields['title'].required = True
form.fields['category'].required = True
# do this for every form field
elif 'mainForm' in request.POST:
form = NewVenueForm(request.POST)
Thanks everyone!!
Multiple Submit buttons in django
In views.py you can pass the button names to your template.Use a dictionary and the render_to_response method provided by django.
in the views.py, in your method (one that takes in a "request" object
from django.shortcuts import render_to_response
....
context_dict = {list1: list1} #just passing in the whole list for your for loop in result_page.html
return render_to_response('<directory_to_your_html_file>/result_page.html', context_dict, context)
Then in your result_page.html
{% for el in list1 %}
<input type="submit" value="{{ el }}"> #double brackets to signify a django variable kinda like {% %}
{% endfor %}
Not sure why so many submit buttons, but the same logic applies if you want to use any other input method (eg radio button, checkboxes)
Hope this helps!
edit: Ok from your added comment it seems like you would benefit from something like
<form id="<a form id>" method="<either "POST" or "GET">" action="<directory to the script you are invoking>">
{% csrf_token %} #don't worry about this one too much, its just django convention
{% for el in list1 %}
<input type="radio" name="a_name" value="{{ el }}">{{ el }}<br> #double brackets to signify a django variable kinda like {% %}
{% endfor %}
<input type="submit" value="Search">
</form>
Try reading on HTML forms http://www.w3schools.com/html/html_forms.asp
Cannot get multiple submit buttons to work in django form
Use the following code
For HTML and Jquery
<form action="/" method="post" id="form">{% csrf_token %}
<!-- {{ form.as_table }} -->
<table>
{% for field in form %}
<tr><td><font color="white">{{field}}</font></td></tr>
{% endfor %}
</table>
<input type="checkbox" name="hidden_checkbox" id="hidden_checkbox" style="display:none"/>
<input type="button" value="Load Entities" data-action="true"/>
<input type="button" value="Export KML" data-action="false"/>
</form>
<script>
$('input[type="button"]').click(function(){
$('#hidden_checkbox').prop('checked', JSON.parse($(this).attr("data-action")));
$('form').submit();
});
</script>
For Django
def globe(request):
if request.method == 'POST':
if request.POST.get('hidden_checkbox'):
print 'LOADED LAYER'
else:
print 'not loaded layer'
Related Topics
Function for Factorial in Python
Why Are Empty Strings Returned in Split() Results
Why Is Parenthesis in Print Voluntary in Python 2.7
How to Properly Round-Up Half Float Numbers
Read and Write CSV Files Including Unicode with Python 2.7
Remove Punctuation from Unicode Formatted Strings
Python/Numpy First Occurrence of Subarray
Embedding a Matplotlib Figure Inside a Wxpython Panel
How to Add Placeholder to an Entry in Tkinter
How to Print Variables Without Spaces Between Values
Pandas: Converting to Numeric, Creating Nans When Necessary
Selenium Compound Class Names Not Permitted
What Is the Internal Precision of Numpy.Float128
Thread Starts Running Before Calling Thread.Start