我在使用 render_template 作为大型程序中函数的一部分时遇到问题。它不会启动我作为参数传递给它的 html 文件。我把所有东西都去掉了,它仍然没有做任何事情。没有收到任何错误,验证了 html 文件是否位于 templates 文件夹中,并通过 app = Flask(name, template_folder='../templates') 指定了文件夹。
以下是Python代码:
from flask import Flask, render_template
from replit import web
app = Flask(__name__, template_folder='../templates')
@app.route('/')
def index():
render_template('renderindex.html',username=web.auth.name,userid=web.auth.user_id)
以下是html代码:
<body>
<main>
<h1>Templating</h1>
<!-- This is the jinja syntax to use variables passed in from render_template. -->
<h2>Hello, {{ username }}!</h2>
<p>Your Replit UserID is {{ userid }}.</p>
</main>
</body>
项目结构:
Documents
static\
templates\
renderindex.html
rendertest.py
您忘记了
return
关键字。
@app.route('/')
def index():
return render_template('renderindex.html',username=web.auth.name,userid=web.auth.user_id)