英文:
TypeError: render_template() flask
问题
这是我的代码,错误出现如下。我不知道为什么。帮帮我。
from flask import url_for, session, Flask, request, redirect, render_template
application = Flask(__name__)
application.secret_key = "sadqwwqds"
@application.route("/")
def home():
if "userID" in session:
return render_template("userid=session.get('username')", login=True)
else:
return render_template(login=False)
@application.route("/login", methods=["get"])
def login():
pass
@application.route("/logout")
def logout():
pass
application.run(host="0.0.0.0")
TypeError: render_template()缺少一个必需的位置参数: 'template_name_or_list'
127.0.0.1 - - [08/Mar/2023 15:28:11] "GET / HTTP/1.1" 500 -
我尝试修改HTML代码,但没有效果。
问题在哪里?
英文:
This is my code and the error appears ad below. i don't know why. help me
from flask import url_for, session, Flask, request, redirect, render_template
application = Flask(__name__)
application.secret_key = "sadqwwqds"
@application.route("/")
def home():
if "userID" in session:
return render_template(userid=session.get("username"), login=True)
else:
return render_template(login=False)
@application.route("/login", methods=["get"])
def login():
pass
@application.route("/logout")
def logout():
pass
application.run(host="0.0.0.0")
TypeError: render_template() missing 1 required positional argument: 'template_name_or_list'
127.0.0.1 - - [08/Mar/2023 15:28:11] "GET / HTTP/1.1" 500 -
{% if login == True%}
<meta charset="utf-8">
<h3>login success {{username}}welcome</h3>
<a href = {{ url_for("logout") }}>logout</a>
{%else %}
<form methods = "get" id="login" action={{url_for("login")}}>
<input type ="id" id ="loginid" name="loginId" placeholder="input">
<input type="password" id="loginPw" name="loginPw" placeholder="input">
<button type="submit" class="login">login</button>
</form>
{%endif%}`
I tried to modify the HTML code, but it din't work
where's the problem?
答案1
得分: 1
Flask的文档指出你需要指定template_name_or_list
(参见https://flask.palletsprojects.com/en/2.2.x/api/#flask.render_template)
这里只指定了关键字参数,其中没有template_name_or_list
:
@application.route("/")
def home():
if "userID" in session:
return render_template(userid=session.get("username"), login=True)
else:
return render_template(login=False)
修改HTML不会有帮助,将模板名称传递给render_template
在你的main.py中。我不知道你的路径,也不知道模板的名称,但带有占位符看起来像这样:
return render_template("<PATH_TO_YOUR_TEMPLATE>/<FILENAME>", userid=session.get("username"), login=True)
英文:
The documentation of Flask states that you have to specify template_name_or_list
(see
https://flask.palletsprojects.com/en/2.2.x/api/#flask.render_template)
Here you specify only keyword arguments, none of which is the template_name_or_list
:
@application.route("/")
def home():
if "userID" in session:
return render_template(userid=session.get("username"), login=True)
else:
return render_template(login=False)
Modifying the HTML won't help, pass the template name to render_template
in your main.py. I don't know your path, neither the name of the template, but with placeholder it looks like this:
return render_template("<PATH_TO_YOUR_TEMPLATE>/<FILENAME>", userid=session.get("username"), login=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论