简体   繁体   中英

Flask: passing multiple python based variables through render_template()

Getting intensively frustrating behavior trying to read two variables that I created in python/a Flask view function.

I have a python program called experiment.py . There's a function that is routed to POST requests and it gets executed when someone submits a form. In that function, I'm trying to return two variables: "user_id" which contains a generated user_id, and "total_trials" which is actually the same as what the user input for the number of trials on the previous form.

I have tried two approaches fore returning these variables. The current approach is using the data parameter in render_template(), and stuffing these variables into a dictionary.

@bp.route('/experiment_app', methods=['GET', 'POST'])
def exp_js_psych_app():
total_trials = int(request.form['trials'])
import random 
import string 

# Generate a random string 
# with 8 characters. 
user_id = ''.join([random.choice(string.ascii_letters 
            + string.digits) for n in range(8)]) 
user_id = str(user_id)
generate_stim_for_trials(total_trials, user_id)
return render_template("index.html", data={"user_id": str(user_id), "total_trials":total_trials})
#return render_template("index.html", user_id=user_id, total_trials=total_trials) # also tried to merely just return both variables individually

So I'm trying to get both of these variables into some inline javascript on index.html. Here's the code.

    <!DOCTYPE html>
<html>
<head>
    <title>My experiment</title>
    <script type="text/javascript">

        // var trial_total = {{ total_trials }};
        var trial_total = {{ data.total_trials }};
        console.log(trial_total);

        // var user_id = {{ user_id }};
        var user_id = String( {{ data.user_id }} ) ;
        console.log("USER ID HTML");
        console.log(user_id);
     </script>
</head>
<body></body>
</html>

You can see I have commented out

I am able to successfully extract the total_trials variable in both approaches, no problem.

The issue comes with user_id. No matter what I do... the {{ data.user_id }} or {{ user_id }} actually does return the sequence of characters that's generated in python. But the problem is that these sequence of characters aren't in a string/have no quotation marks, they just come out like a variable name. So if user_id in the python application is set to a string "12345678", then in Javascript when I use Jinja syntax to reference user_id or data.user_id I am just getting 12345678 , which typically triggers an "unrecognized variable" error.

Here's a screenshot of me using inspect element (trying to check the console) to show what I mean

在此处输入图片说明

If I try to use String , I just get the following: var user_id = String(6z9dVPpG); .

So... What's the issue here?

Note that if I try {{ data.user_id|string }} I still end up with the same thing in my console as included in the screenshot.

sigh

I don't know why it works like this but... var user_id = "{{ data.user_id|string }}"; works. So surrounding the expression with quotation marks.

Are quotation marks necessary when using Jinja expressions to refer to Strings?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM