简体   繁体   中英

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. For that I would want to execute the following Python code:

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

How can I run this code when someone clicks on the DELETE POST button?

Delete button should be linked to a flask route that deletes the a post given a post_name.

example:

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

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