Django: How can I call a view function from template?

后端 未结 6 1928
难免孤独
难免孤独 2020-11-27 12:27

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



        
相关标签:
6条回答
  • 2020-11-27 13:01

    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>
    
    0 讨论(0)
  • 2020-11-27 13:02

    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'),
    
    0 讨论(0)
  • 2020-11-27 13:11

    How about this:

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

    The class is including bootstrap styles for primary button.

    0 讨论(0)
  • 2020-11-27 13:17

    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")
    
    0 讨论(0)
  • 2020-11-27 13:20

    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')
    
    0 讨论(0)
  • 2020-11-27 13:28

    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) }
             });
         });
    });
    
    0 讨论(0)
提交回复
热议问题