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

后端 未结 6 1944
难免孤独
难免孤独 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:28

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

    Something like this:

    (remove the onclick and method)

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

    
    

    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) }
             });
         });
    });
    

提交回复
热议问题