简体   繁体   中英

pass a parameter to a python function in jinja

i have this little issue:

I cannot figure how to give a parameter to a python function inside the jinja scope on onclick function. to explain it better here an example.

Python code (the demineur.onclick function need the x and y values but i cant give them a key argument inside the render)

#! /usr/bin/python
# -*- coding:utf-8 -*-
from flask import Flask,render_template,url_for
app = Flask(__name__)
import demineur
@app.route('/')
def keyIt():
   return render_template('demineur.html',cc=demineur.onclick(x,y)) #i know i cant do that but that what i want to do in reality so i let like that
if __name__ == '__main__':
   app.run(debug=True)

how i want to give the x and y arguments for the python function (there are the i and j ones) HTML

   window.onload=function(){
    tab=document.createElement("table")
    for (i=0;i<20;i++){
        row=tab.insertRow(0)
        for (j=0;j<20;j++){
            cell=row.insertCell(0)
            cell.id=i+j
            cell.setAttribute("onclick","{{cc("+i+","+j+")}}")
        }
    }
    grille=document.getElementById("grille")
    if (!grille.hasChildNodes()) {
        grille.appendChild(tab);
    };

So to pass them to the python function i cant declare the function with directly the x and y parameters because jinja haven't this integrated and i dont want to bother with route because there are simple x and y coordonate and i dont need a fancy thing. Hope you can help.

You will need to pass the x,y arguments in the request.

In case of GET request you can access them with

    x = request.args.get("x")

In case of POST request use

    x = = request.form.get("x")

For details, see the flask.Request documentation

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