Flask-将列表传递到新视图

时间:2019-06-23 00:16:11

标签: python html flask web-applications flask-wtforms

我正在使用Flask和所有相关框架和库(Jinja,WTForms等...)构建一个Web应用程序。这是我第一次使用Flask并构建适当的Web应用程序。我在弄清楚如何访问应用程序目录文件中不同功能的数据时遇到麻烦。

为清楚起见,我想将用户的输入(从HTML表单)传递到生成列表的python函数中,然后将该列表传递回HTML并显示在网页上。

例如:

@app.route("/tool2", methods=['GET', 'POST'])
def tool2():
form = ToolForm()

def mainfunc(var1):

    #Code related to souping

    newdata = []

    for i in list(range(2)):

    maindata = [inte.text for inte in soup.find('div', {'id':'main-content'}).find_all('a', {'class': 'main-info'})]

        if var1 == 'x':
            for i2 in maindata:
                rda = i2 
                newdata.append(rda)
        elif var1 == 'y':
            for i3 in maindata:
                rda = "-" + i3 + "-"
                newdata.append(rda)
        elif var1 == 'z':
            for i4 in maindata:
                rda = '+' + i4 + '+'
                newdata.append(rda)
        else:
            redirect(url_for('tool3')) # <-- This is not working either, if someone wants to be generous and enlighten me on that as well.



        print(newdata)

        testpagecount += 1


if form.validate_on_submit():
    mainfunc(form.tester.data)


return render_template('tool2.html', title='Tool-Active', form=form)

我想通过html在网页上打印newdata,但是如果我尝试调用它,什么也没发生(它不打印,我也没有收到任何错误)。

我的HTML代码是(无论我尝试在html中打印newdata的哪个位置,它都不会打印):

{% extends "layout.html" %}
{% block content %}
<div class="content-section">
    <form method="POST" action="">
        {{ form.hidden_tag() }}
        <fieldset class="form-group">
            <legend>Tester</legend>


            <div class="form-group">
                    {{ form.var1.label(class="form-control-label") }}
                    {% if form.var1.errors %}
                    {{ form.var1(class="form-control form-control-lg is-invalid") }}
                    <div class="invalid-feedback">
                        {% for errors in form.var1.errors %}
                            <span>{{ error }}</span>
                        {% endfor %}
                    </div>
                {% else %}
                    {{ form.var1(class="form-control form-control-lg") }}
                {% endif %}
            </div>


        </fieldset>
        <div class="form-group">
            {{ form.submittool(class="btn btn-outline-info") }}
        </div>
    </form>
</div>
<div class="border-top pt-3">
    <small class="text-muted">
        Learn More <a class="ml-2" href="{{ url_for('about') }}">About</a>
    </small>
</div>
<div class="media-body">

    <div class="article-metadata">
        <a class="mr-2" href="#">{{ newdata }}</a>
        <small class="text-muted">{{ newdata }}</small>
    </div>
<p> {{ newdata }} </p>
</div>
{% endblock content %}

我的问题是,如何将变量从Python函数传递到HTML,并将其显示在网页上?

2 个答案:

答案 0 :(得分:1)

假设您已成功填充newlist,然后在mainfunc内填充,则可以返回以下内容:

return render_template('tool2.html', title='Tool-Active', form=form, mynewlist=newlist)

然后,在您的tool2.html视图内,您可以按以下步骤遍历此返回列表:

<ul>{% for item in mynewlist %}
  <li>item</li>
  {% endfor %}
</ul>

答案 1 :(得分:0)

因此,Flask似乎不喜欢嵌套函数。我可以通过删除条件调用下的函数调用,然后将函数代码置于条件下来使其工作,例如:

if form.validate_on_submit():
    newdata = []

    for i in list(range(2)):

        maindata = [inte.text for inte in soup.find('div', {'id':'main-content'}).find_all('a', {'class': 'main-info'})]

        if var1 == 'x':
            for i2 in maindata:
                rda = i2 
                newdata.append(rda)
        elif var1 == 'y':
            for i3 in maindata:
                rda = "-" + i3 + "-"
                newdata.append(rda)
        elif var1 == 'z':
            for i4 in maindata:
                rda = '+' + i4 + '+'
                newdata.append(rda)
        else:
            redirect(url_for('tool3')) # <-- This is not working either, if someone wants to be generous and enlighten me on that as well.



    print(newdata)

    testpagecount += 1