TypeError: render_template() flask

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

TypeError: render_template() flask

问题

这是我的代码,错误出现如下。我不知道为什么。帮帮我。

  1. from flask import url_for, session, Flask, request, redirect, render_template
  2. application = Flask(__name__)
  3. application.secret_key = "sadqwwqds"
  4. @application.route("/")
  5. def home():
  6. if "userID" in session:
  7. return render_template("userid=session.get('username')", login=True)
  8. else:
  9. return render_template(login=False)
  10. @application.route("/login", methods=["get"])
  11. def login():
  12. pass
  13. @application.route("/logout")
  14. def logout():
  15. pass
  16. 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


  1. from flask import url_for, session, Flask, request, redirect, render_template
  2. application = Flask(__name__)
  3. application.secret_key = "sadqwwqds"
  4. @application.route("/")
  5. def home():
  6. if "userID" in session:
  7. return render_template(userid=session.get("username"), login=True)
  8. else:
  9. return render_template(login=False)
  10. @application.route("/login", methods=["get"])
  11. def login():
  12. pass
  13. @application.route("/logout")
  14. def logout():
  15. 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 -


  1. {% if login == True%}
  2. <meta charset="utf-8">
  3. <h3>login success {{username}}welcome</h3>
  4. <a href = {{ url_for("logout") }}>logout</a>
  5. {%else %}
  6. <form methods = "get" id="login" action={{url_for("login")}}>
  7. <input type ="id" id ="loginid" name="loginId" placeholder="input">
  8. <input type="password" id="loginPw" name="loginPw" placeholder="input">
  9. <button type="submit" class="login">login</button>
  10. </form>
  11. {%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

  1. @application.route("/")
  2. def home():
  3. if "userID" in session:
  4. return render_template(userid=session.get("username"), login=True)
  5. else:
  6. return render_template(login=False)

修改HTML不会有帮助,将模板名称传递给render_template在你的main.py中。我不知道你的路径,也不知道模板的名称,但带有占位符看起来像这样:

  1. 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:

  1. @application.route(&quot;/&quot;)
  2. def home():
  3. if &quot;userID&quot; in session:
  4. return render_template(userid=session.get(&quot;username&quot;), login=True)
  5. else:
  6. 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:

  1. 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:

确定