我正在做一個小專案,我正在從檔案中決議一些 JSON 資料,從 JSON 的每一行中獲取 4 個不同的物件,并使用 FLASK 在網頁中將它們顯示為表格。
其中一個物件是 URL,我想讓它在輸出網頁上“可點擊”、超鏈接。
在燒瓶一側,我有:
@app.route("/")
def table():
for line in filein:
try:
jsonline = json.loads(line)
URL = ("http://myurl.com/" jsonline["name"])
#print(URLToTweet)
objectforlist = (jsonline["time"], jsonline["name"], URL, jsonline["content"])
newdata.append(objectforlist)
except:
pass
return render_template('index.html', headings=headings, data=newdata)
在 HTML 方面,我有:
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in data %}
<tr>
{% for cell in row %}
<td> {{cell}} </td>
{% endfor %}
</tr>
{% endfor %}
</table>
這會在所選 JSON 內容的網頁上創建所需的表格輸出,但 URL 只是文本。我該如何將它們變成超鏈接?我認為它類似于 html 表中的“如果包含 url,則為 xxxxxx”,但我不確定。
uj5u.com熱心網友回復:
由于您為每一行使用一個元組,因此您只需解壓縮回圈內的列即可。元組的各個元素按順序分配給變數。
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for tm, name, url, content in data %}
<tr>
<td>{{tm}}</td>
<td>{{name}}</a></td>
<td><a href="{{url}}" title="{{name}}">Link</a></td>
<td>{{content}}</td>
</tr>
{% endfor %}
</table>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/442478.html
