【问题标题】:Printing more than one image (per line) in flask在烧瓶中打印多个图像(每行)
【发布时间】:2017-07-22 21:55:09
【问题描述】:

我在构建应用程序时遇到问题。基本上它的作用是如果它在字符串(submission.title)中检测到一个国家的名称,那么它将在标题之后打印该国家的国旗。因此,如果标题是“中国正在制造火箭”,那么它就会在火箭这个词之后印上中国国旗。 问题是它不会打印多个标志。因此,如果标题是“中国和俄罗斯正在制造火箭”,它只会打印中国国旗或俄罗斯国旗。不是都。我希望它能够打印两个标志。

谢谢

Python 脚本

news = []
i = 0
for submission in redditFunction(time, limit=int(num) ):
    i += 1
    for j in country: #j = country name
        if j in submission.title: 
            flag = "static/flags/" + country[j].lower() + ".png"
            news.append([str(i) + '. ' + submission.title, submission.url, flag] )
            break
    else:
        news.append([str(i) + '. ' + submission.title, submission.url]) #no flag will be printed

return render_template("index.html", news=news)

HTML

{% for item in news %}                            
     <h2> <a href= "{{item[1]}}">{{item[0]}}</a> <img src= "{{item[2]}}"  style="width:25px;height:18.25px;">  </h2> 
{% endfor %}

【问题讨论】:

    标签: python python-3.x web flask praw


    【解决方案1】:

    我建议将此逻辑移动到您的视图逻辑中并保持您的控制器简单。

    # controller
    return render_template('index.html', 
                           submissions=redditFunction(time, limit=int(num)),
                           countries=countries)
    
    # view 
    {% for submission in submissions %}
    
    <h2>
        <a href="{{ submission.url }}">{{loop.index}}. {{submission.title}}</a>
        {% for country in countries %}
            {% if country in submission.title %}
                {% set country_path = 'flags/%s.jpg' % country %}
                <img src={{ url_for('static', filename=country_path) }}>
            {% endif %}
        {% endfor %}
    
    </h2>
    {% endfor %}
    

    这使您的视图逻辑更易于理解。否则你必须回去重新参考 item[0]item[1] 是什么。您还可以在 Jinja 中获得一些不错的东西,例如 loop.index 用于创建编号标题。

    我在 Flask 中对此进行了测试,它看起来可以正常工作。

    【讨论】:

      【解决方案2】:

      国家/地区循环中的break 阻止您获得所有标志。

      由于每个项目可能有多个标志,您应该使用 list 将相关标志 url 保留在项目对象中(您将其实现为 list

      查看

      news = []
      i = 0
      for submission in redditFunction(time, limit=int(num)):
          i += 1
      
          # flags list will be empty in case of no match
          flags = []
          for j in country: #j = country name -- consider renaming 'j' to 'country_name'
              if j in submission.title:  # you may consider checking with lower()/upper()
                  flags.append("static/flags/" + country[j].lower() + ".png")
      
          news.append([str(i) + '. ' + submission.title, submission.url, flags])
      
      return render_template("index.html", news=news)
      

      在模板中有一个标志列表,您只需要遍历它并为每个标志添加一个 img 元素。

      模板

      {% for item in news %}
          <h2>
              <a href= "{{item[1]}}">{{item[0]}}</a>
              {% for flag in index[2] %}
                  <img src= "{{ flag }}"  style="width:25px;height:18.25px;">
              {% endfor %}
          </h2> 
      {% endfor %}
      

      请注意,我没有测试解决方案,所以如果您遇到错误,请告诉我。

      【讨论】:

        猜你喜欢
        • 2017-06-30
        • 2017-07-24
        • 1970-01-01
        • 2022-10-04
        • 2021-01-20
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 2020-12-25
        相关资源
        最近更新 更多