简体   繁体   English

Python / Flask / Ajax:从客户端向服务器发送字典密钥

[英]Python / Flask / Ajax : send a dict key from client to server

I'm trying to figure out which image has been clicked on my page (generated with Flask) and send the id of this image to a python program with Ajax. 我试图找出页面上单击了哪个图像(使用Flask生成),然后将此图像的ID发送给使用Ajax的python程序。 These images are displayed by a loop like so: 这些图像通过循环显示,如下所示:

_result_remote.html: _result_remote.html:

{% for key, value in resultat.vars_buttons.items() %}
    <div class="tuile{{1+loop.index%23}} news col-xs-4 col-lg-2">
        <img id ="{{key}}"style="margin-top:-5px;" src="../static/img/remote/tvb.png" width="30" >{{ value }}
    </div>
{% endfor %}

I've tried to put this script just before the {% endfor %} statement: 我试着将此脚本放在{%endfor%}语句之前:

$(function() {
    $('img#{{key}}').on("click", function (e) {
        var id = $(this).attr("id");
    $.ajax({
        url: "/remote",
        type: "POST",
        id: id})
    });
});

and finally in my app.py: 最后在我的app.py中:

app.py: app.py:

@app.route('/remote', methods=['POST'])
def remote():
    commande =  request['id'];
    return print(commande)

another information: my page is named _result_remote.html because it's actually an include in another page that is rendered like this: 其他信息:我的页面名为_result_remote.html,因为它实际上是另一个页面中的包含,其呈现方式如下:

app.py: app.py:

@app.route('/')
def accueil():
    resultat = {'module_identique' : "", "module":"", "vars": {}}
    return render_template("accueil_modulable.html", titre="Bienvenue!",resultat=resultat)

accueil_modulable.html: accueil_modulable.html:

<div class="row result {{ resultat.module_identique }}" id="ajax_result">
    {% for module in modules %}
        {% if resultat.module == module %}
            {% include '_result_'+ module +'.html' %}
        {% endif %}
    {% endfor %}
</div>

The include works well by the way. 包括顺便说一句。

Thank you very much for your help 非常感谢您的帮助

If you're using Jinja 2.9 or above you can pass the context variable using with keyword. 如果您使用的是Jinja 2.9或更高版本,则可以使用with关键字传递上下文变量。

<div class="row result {{ resultat.module_identique }}" id="ajax_result">
    {% for module in modules %}
        {% if resultat.module == module %}
            {% with resultat=resultat %}
                {% include '_result_'+ module +'.html'%}
            {% endwith %}
        {% endif %}
    {% endfor %}
</div>

For Jinja versions < 2.9 you'll have to add an extension. 对于<2.9版的Jinja版本,您必须添加扩展名。

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

相关问题 如何使用Flask在Python中将数据从服务器发送到客户端? - How to send data from server to client in Python using Flask? 使用python flask从客户端向服务器发送和接收XML文件 - Send and recieve an XML file from client to server using python flask 如何将信息从客户端发送到服务器(Flask-python) - How to send information from client to server (Flask - python) 数据类型为dict时,Python Flask无法将数据发送到客户端计算机 - Python Flask unable to send data to client machine when datatype is dict 通过ajax从网络摄像头发送imageObject到Flask Server - Send imageObject from webcam by ajax to Flask Server 如何将变量从 ajax 发送到 FLASK python? - how to send variable from ajax to FLASK python? 如何使用 python flask web 服务从服务器发送文件以及如何从客户端获取这些文件? - how to send files using python flask web service from server and how to get those files from client? 如何从Flask服务器(Python)向HTML客户端发送消息? - How do you send messages from Flask server (Python) to HTML client? 如何将数据从android应用程序(客户端)发送到python flask(后端)远程服务器 - How to send data from an android application (client) to a python flask (backend) remote server 将图像从python客户端上传到Flask服务器 - Uploading an image from python client to a flask server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM