简体   繁体   English

将参数传递给jinja中的python函数

[英]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. 我无法弄清楚如何在onclick函数的jinja范围内为python函数提供参数。 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) Python代码(demineur.onclick函数需要x和y值,但我不能在渲染器内给它们一个关键参数)

#! /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 我想如何为python函数提供x和y参数(有i和j)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. 因此,要将它们传递给python函数,我无法直接使用x和y参数声明该函数,因为jinja尚未对此进行集成,并且我不想打扰路线,因为存在简单的x和y协调,而且我不需要花哨的东西。 Hope you can help. 希望能对您有所帮助。

You will need to pass the x,y arguments in the request. 您将需要在请求中传递x,y参数。

In case of GET request you can access them with 如果有GET请求,您可以使用

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

In case of POST request use 如果使用POST请求

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

For details, see the flask.Request documentation 有关详细信息,请参见flask.Request文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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