【问题标题】:Flask Filter Search/List in PythonPython中的烧瓶过滤器搜索/列表
【发布时间】:2021-11-17 16:14:10
【问题描述】:

我在 python 搜索页面部分创建了一个国家列表,数据以 jinja 语法发送到 Html 表“Html 表也有一个 JavaScript 函数,它可以正常运行,不需要帮助”。 我想将数据列表中的数据放在一个单独的文件中,然后将其返回到 python,然后使用现有的 jinja 语法将其发送到 Html 表,并且不更改渲染模板函数中的任何内容。在数据字段中查看一些国家列表的示例,但目的是在其中包含更多,将它们放在单独的文件中并将它们召回到 python 搜索页面然后通过渲染模板函数将它们发送到 Html 表是非常有用的.我知道有一种方法可以通过 JSON 来完成,但据我所知,我无法完成它。关于如何做到这一点的任何想法。谢谢。

Python 示例

# Country page section.
@app.route('/search')
@login_required
def search():

    countryTables = ("Country", "Yearly Water Use", "Daily Water Use", "Population M")

    data = [
        ("Nigeria", "160,470,000,000 mᶟ", "216 liter.p.capital", "216,139,589"),
        ("Ethiopia", "10,550,000,000 mᶟ", "279 liter.p.capital", "122,963,588"),
        ("Egypt", "77,550,100,000 mᶟ", "2,202 liter.p.capital", "120,334,404")
    ]

    return render_template('search.html', countryTables=countryTables, data=data)

HTML 示例

{% extends "layout.html" %}
{% block title %}
    Search Page
{% endblock %}
{% block content %}

<div class="container">
    <br>
    <h3>Country Search!</h3>
    <div class="input-group input-group-lg">
        <input aria-describedby="inputGroup-sizing-lg" aria-label="Sizing example input" class="form-control" id="countryInput"
         onkeyup="countryFunction()" placeholder="Search for Country.." type="text">
    </div>
        <table id="countryTable" class="table table-hover">
            <thead>
                <tr>
                    {% for countryTable in countryTables %}
                    <th scope="col">{{ countryTable }}</th>
                    {% endfor %}
                </tr>
            </thead>

            <tbody>
                {% for row in data %}
                <tr>
                    {% for cell in row %}
                    <td> {{ cell }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}

【问题讨论】:

    标签: javascript python html flask jinja2


    【解决方案1】:

    像这样在 JSON 文件中保存国家/地区详细信息(让我们将其命名为 country.json

    {
        "0": ["Nigeria", "160,470,000,000 mᶟ", "216 liter.p.capital", "216,139,589"],
        "1": ["Ethiopia", "10,550,000,000 mᶟ", "279 liter.p.capital", "122,963,588"],
        "2": ["Egypt", "77,550,100,000 mᶟ", "2,202 liter.p.capital", "120,334,404"]
    }
    

    在您的路线中:

    import json
    
    @app.route('/search')
    @login_required
    def search():
        countryTables = ("Country", "Yearly Water Use", "Daily Water Use", "Population M")
        
        f = open('country.json',)
        countryData = json.load(f)
    
        return render_template('search.html', countryData=countryData, countryTables=countryTables)
    

    在您的 HTML 文件中:

    {% extends "layout.html" %}
    {% block title %}
        Search Page
    {% endblock %}
    {% block content %}
    
    <div class="container">
        <br>
        <h3>Country Search!</h3>
        <div class="input-group input-group-lg">
            <input aria-describedby="inputGroup-sizing-lg" aria-label="Sizing example input" class="form-control" id="countryInput"
             onkeyup="countryFunction()" placeholder="Search for Country.." type="text">
        </div>
            <table id="countryTable" class="table table-hover">
                <thead>
                    <tr>
                        {% for countryTable in countryTables %}
                        <th scope="col">{{ countryTable }}</th>
                        {% endfor %}
                    </tr>
                </thead>
    
                <tbody>
                    {% for row in countryData %}
                    <tr>
                        {% for cell in countryData[row] %}
                        <td> {{ cell }}</td>
                        {% endfor %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endblock %}
    

    【讨论】:

    • 奈坦。感谢您的反馈意见。正如您所写,我尝试了上面的示例。我不断收到错误FileNotFoundError: [Errno 2] No such file or directory: 'country.json' 将文件放在根目录静态文件夹中的哪个文件夹无关紧要我仍然收到相同的错误任何Idee?再次感谢。
    • 您必须根据country.json 文件的位置更改路径。我已将country.json 文件和您的路由文件保存在同一目录中。
    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多