Python 如何在单击按钮时调用 Django 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15341285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:51:37  来源:igfitidea点击:

How do I call a Django function on button click?

pythondjangodjango-templatesdjango-views

提问by Dev

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.

我正在尝试编写一个 Django 应用程序,但我被困在单击按钮时如何调用视图函数。

In my template, I have a link button as below, when clicked it takes you to a different webpage:

在我的模板中,我有一个链接按钮,如下所示,单击它会将您带到不同的网页:

<a target="_blank" href="{{ column_3_item.link_for_item }}">Check It Out</a>

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

单击按钮时,我还想调用 Django 视图函数(以及重定向到目标网站)。视图函数增加数据库中存储按钮被点击次数的值。

The column_3_item.link_for_itemis a link to an external website (e.g. www.google.com). Right now when that button is clicked, it opens a new window which takes you to the google website.

column_3_item.link_for_item是指向外部网站(例如www.google.com)的链接。现在,当单击该按钮时,它会打开一个新窗口,将您带到 google 网站。

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. How I can achieve this?

我想做的是在单击按钮时调用 Django 视图函数,该按钮更新数据库而不刷新页面。我怎么能做到这一点?

采纳答案by furins

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

这是一种纯javascript的简约方法。我使用 JQuery,但您可以使用任何库(甚至根本不使用库)。

<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:

您可以通过以下方式更改链接,而不是放置 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 It Out
</a>

and in your views.py:

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

回答by Mahshid Zeinaly

The following answer could be helpful for the first part of your question:

以下答案可能对您问题的第一部分有所帮助:

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

Django:如何从模板调用视图函数?

回答by Sourabh Sinha

There are 2 possible solutions that I personally use

我个人使用了两种可能的解决方案

1.without using form

1.不使用表格

 <button type="submit" value={{excel_path}} onclick="location.href='{% url 'downloadexcel' %}'" name='mybtn2'>Download Excel file</button>

2.Using Form

2.使用表格

<form action="{% url 'downloadexcel' %}" method="post">
{% csrf_token %}


 <button type="submit" name='mybtn2' value={{excel_path}}>Download results in Excel</button>
 </form>

Where urls.py should have this

urls.py 应该有这个

path('excel/',views1.downloadexcel,name="downloadexcel"),