【问题标题】:Printing a list of tuples in Flask在 Flask 中打印元组列表
【发布时间】:2017-03-18 15:50:35
【问题描述】:

我正在尝试将元组列表打印到我的 html Flask 模板中,但出现此错误:

self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

当我在浏览器中输入localhost 地址时,出现 500 内部服务器错误

如何将我的数据打印到浏览器上?

代码: eslJobs.py

from flask import Flask, render_template    
import bs4 as bs
import urllib.request

app = Flask(__name__)

@app.route('/')
def chinaJobs():
    sauce = urllib.request.urlopen('http://www.eslcafe.com/jobs/china/').read()

    soup = bs.BeautifulSoup(sauce, 'html.parser')

    dl = soup.dl

    chinaAds = []
    china = []

    for words in dl.find_all('a'):
        links = words.get('href')
        link_text = words.text

        if 'university' in link_text.lower():
            chinaAds.append([links, link_text])

        if 'college' in link_text.lower():
            chinaAds.append([links, link_text])

    for ad in chinaAds:
        china.append(tuple(ad))
        return render_template("eslJobs.html", china=china)   

if __name__ == "__main__":
app.run()

代码: eslJobs.html

<!DOCTYPE html>
<html>
<head>
    <title>ESL Jobs</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
</head>
<body>

<div class="page-header">
  <h1>College &amp; University ESL Jobs</h1>
</div>


<ul class="list-group">
{% for href, caption in {{ china }}: %}
  <li class="list-group-item"><a href="{{ href }}">{{ caption }}</a>  </li>
{% endfor %}
</ul>
</body>
</html>

【问题讨论】:

  • 你能发布完整的回溯错误消息吗?我不清楚在哪里将它与你发布的代码相关联。
  • @IronFist 错误信息很长。

标签: python html flask beautifulsoup jinja2


【解决方案1】:

我认为你需要修复这条线:

{% for href, caption in {{ china }}: %}

{% for href, caption in china %}

奖金

这段代码

for words in dl.find_all('a'):
    links = words.get('href')
    link_text = words.text

    if 'university' in link_text.lower():
        chinaAds.append([links, link_text])

    if 'college' in link_text.lower():
        chinaAds.append([links, link_text])

for ad in chinaAds:
    china.append(tuple(ad))  

都可以优化为:

for words in dl.find_all('a'):
    links = words.get('href')
    link_text = words.text
    link_lower = link_text.lower()

    if ('university' in link_lower) or ('college' in link_lower):
        chinaAds.append((links, link_text))

然后只渲染chinaAds:

#fix indentation of your return statement, on your actual code
#it seems belonging to the for loop
return render_template("eslJobs.html", china=chinaAds)

【讨论】:

    【解决方案2】:

    查看jinja document 中的For 用法。

    <h1>Members</h1>
    <ul>
    {% for user in users %}
      <li>{{ user.username|e }}</li>
    {% endfor %}
    </ul>
    

    因此,您不需要在模板中的{% %} 中使用双括号,您可以将其删除。

    {% for href, caption in china : %}
      <li class="list-group-item"><a href="{{ href }}">{{ caption }}</a>  </li>
    {% endfor %}
    

    在您的 python 脚本中,有一个缩进错误,导致您无法获取完整列表。

    for ad in chinaAds:
        china.append(tuple(ad))
    return render_template("eslJobs.html", china=china)  
    

    【讨论】:

    • 哈哈!好的。棒极了。我还添加了另一个 for 循环以打印出完整列表。谢谢!
    • 我已经修改了我的答案。您可以获取完整列表。
    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多