Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a Django function on button click?

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

The column_3_item.link_for_item is 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.

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?

like image 559
Dev Avatar asked Mar 11 '13 14:03

Dev


People also ask

How can call Django function on button click in HTML?

The view function increments the value in the database which stores the number of times the button is clicked. The column_3_item. link_for_item is 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.

How do you call a view function in Django HTML?

To call a view function from template with Python Django, we can add a link to the URL of the view function. to add a link to the view with name delete_product in admin_page. html. to add the path to the delete_product view.

What is Django template language?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.


1 Answers

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')) 
like image 169
furins Avatar answered Oct 05 '22 04:10

furins