34

I have a question on how to call a view function from a template HTML button? Like an onclick function? Here is the template:

<input id="submit" type="button" onclick="xxx" method="post" value="Click" />

And the views.py is:

def request_page(request):
    ...do something...
    return render_to_response("/directory.html", {})

Thank you very much.

Robert
  • 1,979
  • 6
  • 30
  • 38

6 Answers6

111

Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'Click' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.

inside templateHTML.html:

<form action="#" method="get">
 <input type="text" value="8" name="mytextbox" size="1"/>
 <input type="submit" class="btn" value="Click" name="mybtn">
</form>

inside view.py:

import mypythoncode

def request_page(request):
  if(request.GET.get('mybtn')):
    mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
return render(request,'myApp/templateHTML.html')
Sarthak Agrawal
  • 196
  • 3
  • 15
Mahshid Zeinaly
  • 3,300
  • 6
  • 23
  • 31
16

One option is, you can wrap the submit button with a form

Something like this:

<form action="{% url path.to.request_page %}" method="POST">
    <input id="submit" type="button" value="Click" />
</form>

(remove the onclick and method)

If you want to load a specific part of the page, without page reload - you can do

<input id="submit" type="button" value="Click" data_url/>

and on a submit listener

$(function(){
     $('form').on('submit', function(e){
         e.preventDefault();
         $.ajax({
             url: $(this).attr('action'),
             method: $(this).attr('method'),
             success: function(data){ $('#target').html(data) }
         });
     });
});
karthikr
  • 92,866
  • 25
  • 190
  • 186
  • 3
    Yeah.. thats what the ajax submit is doing. your question was unclear, so i added both the answers – karthikr Jul 11 '13 at 18:26
  • I wonder is there any way for doing multithreadings in Django? Here is the questions: http://stackoverflow.com/questions/17601698/can-django-do-multi-thread-works – Robert Jul 11 '13 at 19:26
  • [Django-Notification](https://github.com/pinax/django-notification) could be what you are looking for. What does this have to do with this question anyways ? – karthikr Jul 11 '13 at 19:29
  • @karthikr you'll have to change your input in the first snippet to type="submit" – Kelsey Dec 30 '16 at 03:01
  • Where do I put the code for the submit listener? – Uwe.Schneider Apr 17 '22 at 13:46
12

How about this:

<a class="btn btn-primary" href="{% url 'url-name'%}">Button-Text</a>

The class is including bootstrap styles for primary button.

mapadj
  • 181
  • 2
  • 3
5

you can put the input inside a form like this:-

<script>
    $(document).ready(function(){
        $(document).on('click','#send', function(){
            $('#hid').val(data)
            document.forms["myForm"].submit();
        })
    })
</script>

<form id="myForm" action="/request_page url/" method="post">
    <input type="hidden" id="hid" name="hid"/>
</form>
<div id="send">Send Data</div>
Nullify
  • 19,655
  • 7
  • 34
  • 59
4

For example, a logout button can be written like this:

<button class="btn btn-primary" onclick="location.href={% url 'logout'%}">Logout</button>

Where logout endpoint:

#urls.py:
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
Nitwit
  • 232
  • 3
  • 11
Lucian
  • 824
  • 11
  • 31
3

For deleting all data:

HTML FILE

  class="btn btn-primary" href="{% url 'delete_product'%}">Delete

Put the above code in an anchor tag. (the a tag!)

url.py

path('delete_product', views.delete_product, name='delete_product')]

views.py

def delete_product(request):
    if request.method == "GET":
        dest = Racket.objects.all()
        dest.delete()
        return render(request, "admin_page.html")
bagerard
  • 3,772
  • 2
  • 21
  • 34