簡體   English   中英

取決於我剛剛從 sqlite 數據庫表中查詢的列的值,我如何在 HTML 文件上顯示不同的圖標?

[英]Depend on the the values of the column I just query from the sqlite database's table, how could I show different icons on my HTML file?

我有下面的查詢命令(在 Python 中),稍后將用於分頁並顯示我網站上的所有博客:

@core.route('/', methods=['GET', 'POST'])
def index():

    form = Blogsearch_form(request.form)

    page = request.args.get('page',1,type=int)
    many_posts0 = BlogPost.query.order_by(BlogPost.date.desc())
    many_post = many_post0.paginate(page=page, per_page=10)
    return render_template('index.html', many_posts=many_posts, form=form)

我的 BlogPost 表有一個名為“problem_type”的列,其中有一些值,如教育、健康……我想知道是否有任何代碼可以幫助我根據“problem_type”中的值顯示教育、健康圖像圖標“ 柱子。

下面的代碼是錯誤的,但我想寫一些代碼,比如:

if many_posts0(BlogPost.problem_type) == "education":
             icon_link = '../static/education.jpg'

if many_posts0(BlogPost.problem_type) == "health":
             icon_link = '../static/health.jpg'


這是我的 HTML 文件的重要部分:

<div class="container row row-cols-1 row-cols-md-2 text-center">
{% for post in many_posts.items%}

    <div class="card border-dark mb-3 " style="width: 20rem;">
     <div class="card-body ">
         <p class="text-primary text-left">#{{ post.problem_type }}</p>
         <h7><a class="text-warning" href="{{ url_for('users.user_posts', username=post.creator.first_name+post.creator.middle_name+post.creator.last_name) }}"><img class="text-center rounded" src="{{ url_for('static', filename='profile_pics/'+ post.creator.profile_image) }}" width = "35" height = "35" alt=""> {{ post.creator.first_name}} {{ post.creator.middle_name }} {{ post.creator.last_name }}</a></h7>
         <p></p>
         <img class="text-center rounded responsive1" alt="" src="{{ url_for('static', filename='blog_pics/'+ post.blog_image) }}" width = "495" height = "250">
         {# Need caution for post.blog_image on the code above #}
         <p></p>
         <h2><a class="card-tittle text-body problem" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">{{ post.problem_name[0:40]}}..</a></h2>
         <p class="card-text">{{ post.text[0:100] }}</p>
         <p><small class="text-muted">Posted on: {{ post.date.strftime('%Y-%m-%d') }}</small></p>
         <a class="btn btn-warning" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">Read more</a>
     </div>


    </div>

{% endfor %}

正如您在上面看到的,我可以使用 {{post.problem_type}} 顯示每個帖子的“problem_type”值,但我希望下面的內容顯示圖標取決於其“problem_type”id:


{{post.problem_type}} <img src=”{{icon_link}}}” width = "30" height = "30" alt="">

我是一個初學者,我正在努力解決這個問題。 如果您能幫助我,我將不勝感激。 謝謝!

Ps/順便說一句,我正在使用 sqlalchemy 從 SQLite 數據庫中查詢。 我也在使用 Flask。

{% if post.problem_type == 'X' %}
     <img src={URL for problem_type 'X'}....>
{% elif post.problem_type == 'Y' %}
     <img src={URL for problem_type 'Y'}....>
{% else %}
     <img src={URL for general image}....>
{% endif %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM