[Answer]-Multiple Submit buttons in django

1👍

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

👤Fan Ma

Leave a comment