简体   繁体   English

单击按钮时,如何在 jinja 模板中运行 Python 代码?

[英]How can I run Python code in a jinja template when a button is clicked?

<html>
    <head>

    </head>
    <body>
        {% for post in posts %}
            {% if loop.changed(current_post) %}
            <div>
                <a href="{{ url_for('read', post_name=post.post_name) }}">
                  <img src="{{ url_for('static', filename='images/'+post.title) }}" alt="">
                </a>
              </div>
              {% if current_user.is_moderator %}
                <div>
                  <button>DELETE POST</button>
                </div>
              {% endif %}
              <!-- <div>{{ current_post }}</div>
                <div>{{ current_post }}</div> -->
            {% else %}
                {% set current_post = post.post_name %}
                <div></div>
            {% endif %}
        {% endfor %}
    </body>
</html>

Basically, I want to delete the post whose post_name value is stored inside the current_post variable when the DELETE POST button is clicked.基本上,我想在单击 DELETE POST 按钮时删除其 post_name 值存储在 current_post 变量中的帖子。 For that I would want to execute the following Python code:为此,我想执行以下 Python 代码:

Image.query.filter_by(post_name=current_post).delete() 

How can I run this code when someone clicks on the DELETE POST button?当有人单击 DELETE POST 按钮时,如何运行此代码?

Delete button should be linked to a flask route that deletes the a post given a post_name.删除按钮应链接到烧瓶路线,该路线删除给定 post_name 的帖子。

example:例子:

<a href="{{ url_for('delete_post', post_name=current_post.post_name) }}">
    DELETE POST
</a>

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

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