How do I call a Django function on button click?

前端 未结 3 1501
走了就别回头了
走了就别回头了 2020-12-02 10:10

I am trying to write a Django application and I am stuck at how I can call a view function when a button is clicked.

In my template, I have a link button as below, w

3条回答
  •  情歌与酒
    2020-12-02 10:43

    here is a pure-javascript, minimalistic approach. I use JQuery but you can use any library (or even no libraries at all).

    
        
            An example
            
            
        
        
            
            
        
    
    

    Alternative approach

    Instead of placing the JavaScript code, you can change your link in this way:

    
        Check It Out
    
    

    and in your views.py:

    def YOUR_VIEW_DEF(request, pk):
        YOUR_OBJECT.objects.filter(pk=pk).update(views=F('views')+1)
        return HttpResponseRedirect(request.GET.get('next')))
    

提交回复
热议问题