[Python] How do you pass a variable from javascript to python (oTree) or django?

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I have some created some variables in Javascript to tracker how long an individual holds a button. I would like to pass these variables within a class in python (oTree) to store the data.

    So, suppose the Javascript code is as follows:

    let javascriptvariable = 0;


    This javascriptvariable will be updated as the user clicks the button, so let's say the final value is javascriptvariable = 100. The python code is below.

    class DisplayCharacteristics2(Page):
    form_model = 'player'
    pythonvariable = javascriptvariable


    @staticmethod
    def vars_for_template(player: Player):
    return {
    "var1": player.var1,
    }


    I tried created an empty variable, but I get an error which says something like javascript can't "add" to a null variable. I also tried using django in combination with javascript (see below), but javascript cannot add to a null variable. I tried to let it equal to zero first, but then I only get data points of 0.

    let javascriptvariable = `{{ pythonvariable|safe }}`


    Any leads will be appreciated! Thanks in advance.

    Continue reading...

Compartilhe esta Página