Using JavaScript Onclick Event to pass Data to in Django?

Tags: , , , ,

I Have a bit of idea about this to use ajax with the JavaScript to send data to but I am not able to do that.

So what I am trying to do is I have put onclick event on the image so by clicking on that image I should be able to send some value to

This is Hello.html file:

<img src="" onclick="change()">

JavaScript function:

function change(){
     // Do something here to send data to

Now in

def SomeFunction():
    //To get data here


You can use ajax function :


<img src="" onclick="change(foo, bar)">

javaScript :

function change(foo, bar){
    url: 'ajax/foo/',
    data : {
        'foo': foo,
        'bar': bar
    success: function (data) {

} :

def SomeFunction(request):
foo = request.GET.get('foo')
foo = request.GET.get('bar')

path('ajax/foo/', views.SomeFunction, name='ajax_foobar'),

Source: stackoverflow