TypeError: render_template() flask

huangapple go评论57阅读模式
英文:

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(&quot;/&quot;)
def home():
    if &quot;userID&quot; in session:
        return render_template(userid=session.get(&quot;username&quot;), 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(&quot;&lt;PATH_TO_YOUR_TEMPLATE&gt;/&lt;FILENAME&gt;&quot;, userid=session.get(&quot;username&quot;), login=True)

huangapple
  • 本文由 发表于 2023年3月8日 15:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定