簡體   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>

基本上,我想在單擊 DELETE POST 按鈕時刪除其 post_name 值存儲在 current_post 變量中的帖子。 為此,我想執行以下 Python 代碼:

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

當有人單擊 DELETE POST 按鈕時,如何運行此代碼?

刪除按鈕應鏈接到燒瓶路線,該路線刪除給定 post_name 的帖子。

例子:

<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