N
N
Nika Savchenko2019-07-25 15:18:22
Django
Nika Savchenko, 2019-07-25 15:18:22

How to do a button click check in django?

I have an html code in which I need to find out which button the user clicked and perform certain actions.
HTML:

<div class="w3-show-inline-block">
    <div class="w3-bar">
        <h4 class="w3-wide">Сортировать по:
        <button name="sort_complexity_ascending" class="w3-button w3-black">сложности от 1 до 5</button>
        <button name="sort_complexity_descending" class="w3-button w3-black">сложности от 5 до 1</button>
        <button name="sort_alphabet_ascending" class="w3-button w3-black">алфавиту от А до Я</button>
        <button name="sort_alphabet_descending" class="w3-button w3-black">алфавиту от Я до А</button></h4>
    </div>
</div>

views.py:
if request.method == 'GET':
        if request.GET.get("sort_alphabet_ascending"):
            history = sort_alphabet_ascending(history)
        elif request.GET.get("sort_alphabet_descending"):
            history = sort_alphabet_descending(history)
        elif request.GET.get("sort_complexity_descending"):
            history = sort_complexity_descending(history)
        elif request.GET.get("sort_complexity_ascending"):
            history = sort_complexity_ascending(history)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-07-25
@sergey-gornostaev

The path of a web developer must begin with the understanding that a website is not really one application, but two, written in different languages, running on different computers, in different environments and at different times.
In the case of Django, the operation of the site can be broken down into the following steps:
All further user actions will be processed only in the browser, by the browser itself and/or javascript. Both javascript and the browser will work with the data received from the server. If you need other data or you need to transfer some data to the server, you will have to repeat the operations from the list above, either by submitting a form or using an ajax request.
Hence the answer to your question, if you want Django to respond to a button click, you'll have to send it an http request, either via a form or via AJAX.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question