下面的代码在.py文件中运行得非常好,但我想分离HTML并将其放入templates/index.HTML中
我想我必须使用Flask中的render_template函数才能返回相同的结果
# File dynamic_website.py
from owlready2 import *
onto = get_ontology("bacteria.owl").load()
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')
def ontology_page():
html = """<html><body>"""
html += """<h2>'%s' ontology</h2>""" % onto.base_iri
html += """<h3>Root classes</h3>"""
for Class in Thing.subclasses():
html += """<p><a href="%s">%s</a></p>""" % (url_for("class_page", iri = Class.iri), Class.name)
html += """</body></html>"""
return html
我创建了一个文件夹模板和一个index.html文件。我使用了return render_模板('index.html'),但它不起作用。我必须向return_template函数添加哪些参数?“对于Thing.subclass()中的类:”“必须在.html文件或.py文件中?”?函数的url_如何
如果您可以编辑.py代码并让我知道我应该在index.html文件中准确地编写什么以获得相同的结果,那将是非常棒的
更新:
我所做的:
from flask import Flask, render_template
from owlready2 import *
from flask import Flask, url_for
onto = get_ontology("bacteria.owl").load()
app = Flask(__name__)
@app.route("/")
def ontology_page():
for Class in Thing.subclasses():
return render_template('index.html')
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>{{ Class.name }}</h1>
</body>
</html>
不能多次返回函数。返回的是函数的值This tutorial在JS中,但它暗示了与python相同的概念
如果您希望用户看到html上的内容列表,请执行以下操作
render_template('index.html', things=Thing.Subclasses())
这将给Jinja一个列表,它可以在其中执行for循环对于html,您可以这样做
{% for s in things %} {{ s }} is something {% endfor %}
。尽管s
是列表中的一个子类,但可以对s
执行任何操作相关问题 更多 >
编程相关推荐