Python Flask jinja2 模板变量上下文。无法渲染变量

标签 python python-2.7 flask jinja2

编辑:简化了 for 循环以更容易说明问题,但问题仍然相同。

我正在尝试渲染 Flask jinja2 模板来渲染图像名称列表中的图像轮播。 images 变量显示在我的调试器中,当我单步执行它时,它看起来像是在循环,但由于某种原因它没有渲染。

# flask app file
@app.route('/results', methods=['POST', 'GET'])
@login_required
def newpage(page=1):
    """
    Renders search page
    """
    form = genus_form()
    user = current_user
    if request.method == 'POST':
        if form.validate_on_submit():
            gen = Genus.query.filter(Genus.genus_name.like(form.genus.data)).first()
            spec = Species.query.filter_by(GID=gen.GID).first()
            images = Images.query.filter_by(UID=spec.UID).all()
            if images:
                images = [i.FileName for i in images]
            form.genus.data = ''
            render_template('results.html', form=form, images=images)
        else:
            return render_template('results.html', form=form)
    return render_template('results.html', form=form)

以下是 jinja 模板中的相关部分:

{% extends 'base.html' %}
{% block title %} WCTC Image Review {% endblock %}
{% block scripts %}
    {{ super() }}
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>

$(document).ready(function () {

    $("#genus").autocomplete({
        delay: 100,
        source: function (request, response) {

            // Suggest URL
            var suggestURL = "{{ url_for('autocomplete') }}?term=%QUERY";
            suggestURL = suggestURL.replace('%QUERY', request.term);

            // JSON Request
            $.ajax({
                method: 'GET',
                dataType: 'json',
                jsonCallback: 'jsonCallback',
                url: suggestURL
            })
            .success(function(data){
                response(data);
            });
        }
    });
});

$('#myCarousel').carousel({
  interval: 40000
});

$('.carousel .item').each(function(){
  var next = $(this).next();
  if (!next.length) {
    next = $(this).siblings(':first');
  }
  next.children(':first-child').clone().appendTo($(this));

  if (next.next().length>0) {
      next.next().children(':first-child').clone().appendTo($(this)).addClass('rightest');

  }
  else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));

  }
});

</script>
{% endblock %}
{% block content %}

<div>
    <form action="/results" method="POST" id="search">
        <fieldset>
            <div>
                {{ form.genus(placeholder="Genus") }}
            </div>
            <div>
                <button type="submit" class="btn btn-default">Submit</button>
            </div>
        </fieldset>
    </form>
</div>
{% if images %}
                {% for filename in images %}
                         <div>
                             {{ filename }}
                         </div>
                {% endfor %}
{% endif %}
{% endblock %}

任何帮助将不胜感激。

最佳答案

return 添加到此行:

return render_template('results.html', form=form, images=images)

模板应该是这样的:

{% if images %}
    {% for filename in images %}
        <div>
          <img src="{{ filename }}">
        </div>
    {% endfor %}
{% endif %}

关于Python Flask jinja2 模板变量上下文。无法渲染变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988534/

相关文章:

python - 遍历包含重复值的列表

python - Flask-SQLAlchemy 不使用 create_all() 创建表

javascript - Flask + Ajax 集成 : AttributeError: 'WSGIRequestHandler' object has no attribute 'environ'

javascript - onclick 加载 {% include 'test.html' %}

python - Pyinstaller - 从最终可执行文件中排除文件

android - Kivy-即使安装了32位库也无法执行Aidl

python - pandas 将行数据转换为列数据

python - 将字典的字典打印为表格

python-2.7 - 为什么 ElementTree 拒绝带有 "encoding incorrect"的 UTF-16 XML 声明?

python-2.7 - IndexError : index out of bound