python - django multiple submit buttons -


i have many buttons in django template. each button has different name {{transaction.id}}. how can refer (submit) single button in view?

base.html

<h3>open transactions</h3>     {% transaction in transactions_open %}     {{ transaction.id }} {{transaction.timestamp}} {{ transaction.currency }} {{ transaction.sell_or_buy }} {{ transaction.bid }} {{ transaction.date_add }}     <form action="" method="post">     {% csrf_token %}     <button type="submit" name={{ transaction.id }} value="close">close</button>     </form>     </p>     {% endfor %} 

views.py

class mainview(view):  def get(self, request):      transactions_open = dealmodel.objects.filter(open_or_closed='open')     transactions_closed = dealmodel.objects.filter(open_or_closed='closed')     content_dict = {'transactions_open': transactions_open,                     'transactions_closed': transactions_closed}     return templateresponse(request, 'base.html', content_dict)  def post(self,request):      if request.method == 'post':         transactions_open = dealmodel.objects.filter(open_or_closed='open')         transactions_closed = dealmodel.objects.filter(open_or_closed='closed')          if request.post.get('name???') == 'close':         content_dict['answer'] = "closed!!!"         return templateresponse(request, 'base.html', content_dict) 

this example

first

def post(self,request): called when request method post don't need check it


second

this solution depends on got question
don't need form have close button can handle using javascript if want hide it

<ul>{% transaction in transactions_open %} <li class="close_button" data-pk="{{ transaction.id }}">{{ transaction.id }} {{transaction.timestamp}} {{ transaction.currency }} {{ transaction.sell_or_buy }} {{ transaction.bid }} {{ transaction.date_add }}  <a type="submit"  value="close">close</a>  </li> {% endfor %}</ul> 

at javascript file using jquery

$('.close_button a').onclick(function(){      $(this).parent(".close_button").hide(); // $.ajax{             url: "{% url 'app_label:view_name' %}",             data: {'data':$(this).data("pk")},             method: "post",             success: function (result,status,xhr) {                  alert('closed successfully.');              },  }) 

handle closing transaction @ view:

class myview(view):     def post(self, request, *args, **kwargs):         if request.is_ajax():              id = request.post.get("pk")              transaction.objects.get(id=id).close() #develop close method @ model classes  

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -