如何调用django功能按钮点击

如何调用django功能按钮点击

本文介绍了如何调用django功能按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Django应用程序,当我点击一个按钮时,我可以调用视图功能。



在我的模板中,我有一个链接按钮如下所示,当您点击到另一个网页。

 < a target =_ blankclass =btn btn-info pull-righthref ={{column_3_item.link_for_item}}>检查出< / a> 

当点击按钮时,我还要调用Django视图功能(以及重新直接到目标网站)。视图功能是增加存储按钮被点击次数的数据库中的值。



任何人都可以帮助我如何实现这一点。 p>

编辑: -



抱歉的困惑。 column_3_item.link_for_item是指向外部网站的链接(例如:www.google.com)。现在当该按钮被点击时,它打开一个新的窗口,它需要谷歌网站。我想要做的是在点击按钮更新数据库而不刷新页面的同时调用django视图函数。



谢谢

解决方案

这里是一个纯JavaScript,简约的方法。我使用JQuery,但您可以使用任何库()。

 < html> 
< head>
< title>示例< / title>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js>< / script>
< script>
函数call_counter(url,pk){
window.open(url);
$ .get('YOUR_VIEW_HERE /'+ pk +'/',function(data){
alert(counter updated!);
});
}
< / script>
< / head>
< body>
< button onclick =call_counter('http://www.google.com',12345);>
我更新对象12345
< / button>
< button onclick =call_counter('http://www.yahoo.com',999);>
我更新对象999
< / button>
< / body>
< / html>






替代方法 / p>

而不是放置JavaScript代码,您可以通过以下方式更改链接:

 < a target =_ blank
class =btn btn-info pull-right
href ={%url YOUR_VIEW column_3_item.pk%} /?next = {{column_3_item .link_for_item |进行urlencode: ''}}>
Check out Out
< / a>

和您的 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')))


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 when clicked takes you to a different webpage.

<a target="_blank" class="btn btn-info pull-right" href="{{ column_3_item.link_for_item }}">Check It Out</a>

When the button is clicked, i want to call a Django view function also (along with re-direct to a target website). The view function is to increment the value in the database which stores the number of times the button is clicked.

Could anyone please help me how i can achieve this.

Edit :-

sorry for the confusion. the column_3_item.link_for_item is a link to an external website (example:- www.google.com). Right now when that button is clicked, it opens a new window which takes to google website. what i would like to do is to call a django view function also when the button is clicked which updates the database without refreshing the page.

Thanks

解决方案

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

<html>
    <head>
        <title>An example</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>
            function call_counter(url, pk) {
                window.open(url);
                $.get('YOUR_VIEW_HERE/'+pk+'/', function (data) {
                    alert("counter updated!");
                });
            }
        </script>
    </head>
    <body>
        <button onclick="call_counter('http://www.google.com', 12345);">
            I update object 12345
        </button>
        <button onclick="call_counter('http://www.yahoo.com', 999);">
            I update object 999
        </button>
    </body>
</html>


Alternative approach

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

<a target="_blank"
    class="btn btn-info pull-right"
    href="{% url YOUR_VIEW column_3_item.pk %}/?next={{column_3_item.link_for_item|urlencode:''}}">
    Check It Out
</a>

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')))

这篇关于如何调用django功能按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:19