React to user interaction

Hello my Django friends,
I’ve now worked my way through the Django documentation and I have the feeling - this is going to be tough.

At the moment I am not sure how my Pythen code can communicate with an “interaction” on the website - clearly via “Get” and “Post”, I understood that, but what about a click of a button?

For example, if I have a checkbox (or a button, etc.):

<input id="checkbox" type="checkbox" name="myTextEditBox" value="checked">

Then I could create an EventListener via JavaScript:

<script>
        document.addEventListener("DOMContentLoaded", () => {
            const checkbox = document.querySelector("#checkbox")
            checkbox.addEventListener("click", () => {
                console.dir(Hello Python - the CheckBox was clicked here?)
            })
        })
    </script>

But how can I react to the event in Python - in my view.py? Is there a simple explanation for this?

I have already tried to solve this with an onclick function - but here too I was at a loss as to how to “activate” my function in Python.

It would be helpful for me to have a small (simple) example of what the python code would look like - if that is possible

< onclick="checkbox()" input id="checkbox" type="checkbox" name="myTextEditBox" value="checked">
<script>
       function checkbox{
        document.addEventListener("DOMContentLoaded", () => {
        const checkbox = document.querySelector("#checkbox")
        checkbox.addEventListener("click", () => {
            console.dir("And now?")
        })
    })
 }
</script>

Thanks very much :slight_smile:

Simple explanation? You can’t - not directly.

Django, and therefore the python code, runs on the server. By the time the page has been sent to the browser and the JavaScript has become active, the Django view is done and out of scope.

If you need to perform some action on the server from the browser, you would do it using JavaScript / AJAX to invoke a view. That view can then return data to JavaScript for it to do “whatever” with it.

Okay great, thank you very much.

Since I am currently not yet familiar with AJAX, I have to read here first.

I could solve my current problem with the following code:

    if request.method == "POST":
        Button1 = request.POST.get("button1", False)
        Button2 = request.POST.get("button2", False)

        if Button1 == "button1":
        ........

This allows me to react to different clicks - I am now wondering whether this is a “clean” solution or whether this is a method that should not be used.

You’ve not provided enough details and specifics about the page being rendered, the form being submitted, and what you’re trying to do with this once it’s submitted to the server for us to make a comment.

You said that

Does that include working your way through the official Django tutorial? If so, then you’ve been exposed to working with Django forms. That’s the appropriate way to be working with data submitted from a page. (Also see Working with forms | Django documentation | Django and the pages it refers to for more details.)