Home > Blockchain >  React to "click event" in Python (Django)
React to "click event" in Python (Django)

Time:11-16

everyone, beforehand - I'm a bloody but motivated developer beginner.

I am currently trying to react to simple events (click on a button) in the HTML code in my Django project. Unfortunately without success ...

HTML:

<form>
    {% csrf_token %}
    <button id="CSVDownload" type="button">CSV Download!</button>
</form>

JavaScript:

    <script>
        document.addEventListener("DOMContentLoaded", () => {
            const CSVDownload = document.querySelector("#CSVDownload")
            CSVDownload.addEventListener("click", () => {
                console.dir("Test")
            })
        })
    </script>

Do I need JavaScript for this? Or is there a way to react directly to such events in python (Django)?

I am really grateful for all the support.

Since I'm not really "good" yet - a simple solution would be great :)

Python (Django)

    if request.method == "POST":
        projectName = request.POST["projectName"]
        rootDomain = request.POST["rootDomain"]
        startURL = request.POST["startURL"]
....

With this, for example, I managed to react to a kind of event, i.e. when the user sends the form. The problem here, however, is - if I have several forms on one page, then I cannot distinguish which function should be carried out: / I am at a loss

CodePudding user response:

This World help you HTML

<form>
    {% csrf_token %}
    <button id="CSVDownload" type="button" onclick="download_csv()">CSV Download!</button>
</form>

Javascript

<script>
       function download_csv{
        document.addEventListener("DOMContentLoaded", () => {
        const CSVDownload = document.querySelector("#CSVDownload")
        CSVDownload.addEventListener("click", () => {
            console.dir("Test")
        })
    })

 }
    
</script>

It might be error while writing the function but this is the way how you react by javascript to the click using on click method

CodePudding user response:

Do I need JavaScript for this? Or is there a way to react directly to such events in python (Django)?

I would say that for now JavaScript is the best avenue for you to explore, as whilst there are various ways of getting python to run on the client side (i.e. in your browser), I wouldn't necessarily recommend them to a beginner. Or rather, interpolating them into a Django project might be challenging.

Skulpt

Brython

  • Related