How can code be activated on click of a button in a Django view?

I have a template that contains a button.

I have a view for this template in a view file.

How can a part of the code be activated when this button is clicked - which is in the template?

How can you make this as easy as possible?

How can code be activated on click of a button in a Django view?

def click_button_summ(request):
    context = {}
    if request.POST.get("button_table_summ") == "button_table_summ":
        ///code///
    return render(request, "click_button_summ.html", context)


    <div class="ui container">
        <div class="column">
            <button type="submit" class="ui button" name="button_table_summ" value="button_table_summ">Button</button>
        </div>
    </div>`

Buttons aren’t clicked in a Django view. Buttons are clicked on a web page.

That click can cause the button to issue an http request.

The url in that request is looked up in the urls.py files to find the appropriate view.

The view is called with the request as a parameter.

The view returns a response.

The browser is responsible for handling that response.

So any html that results in the generation of a request will work for you. It’s then more of a question of what you want the response to be, and how the browser is going to handle it. (For example, what problems might be caused by a user clicking the button twice in a row?)

2 Likes