简体   繁体   English

在Flask中打印元组列表

[英]Printing a list of tuples in Flask

I am trying to print a list of tuples into my html Flask template, but I am getting this error: 我正在尝试在我的html Flask模板中打印一个元组列表,但出现此错误:

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

I get a 500 Internal Server Error when I put the localhost address in my browser localhost地址放入浏览器时出现500内部服务器错误

How can I print my data onto the browser? 如何将数据打印到浏览器上?

CODE: eslJobs.py 代码: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()

CODE: eslJobs.html 代码: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>

See the For usage in jinja document . 请参阅For在使用jinja文件

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

Therefore, you don't need the double brace in your template where inside a {% %} , you can just delete it. 因此,您不需要模板中的双括号,只需在{% %}内将其删除即可。

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

In your python script, there is an indentation error, that prevent you from getting the full list. 在您的python脚本中,有一个缩进错误,它使您无法获取完整列表。

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

I think you need to fix this line: 我认为您需要解决以下问题:

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

to

{% for href, caption in china %}

Bonus : 奖励

This code 这段代码

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))  

can all be optimized to: 可以全部优化为:

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))

Then just render chinaAds : 然后只渲染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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM