gpt4 book ai didi

python - jinja2.exceptions.UndefinedError - 没有属性 'favicon.ico'

转载 作者:行者123 更新时间:2023-11-28 18:19:31 27 4
gpt4 key购买 nike

我正在尝试运行我的 flask 应用程序 - 昨晚运行良好。但是,今天早上,尝试运行相同的代码(但是在另一台计算机上)时,出现错误:

jinja2.exceptions.UndefinedError: 'flask.sessions.SecureCookieSession object' has no attribute 'favicon.ico'

完整回溯:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users//Desktop/survey-system/scripts/routes.py", line 88, in course_survey
return render_template("survey.html", course=course)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Users//Desktop/survey-system/templates/survey.html", line 10, in top-level template code
{% for q, a in zip(session[course]["questions"], session[course]["answers"]) %}
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 411, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'flask.sessions.SecureCookieSession object' has no attribute 'favicon.ico'

survey.html

   <body>
<h1 align="left"> This is the survey for {{course}}</h1>
<form>
{% for q, a in zip(session[course]["questions"], session[course]["answers"]) %}
... do stuff ...

routes.py(相关代码)

@app.template_global(name='zip')
def _zip(*args, **kwargs): # So that jinja may use the zip function
return zip(*args, **kwargs)

@app.route('/<string:course>', methods=['GET', 'POST'])
def course_survey(course):
return render_template("survey.html", course=course)

@app.route('/questionbank', methods=['GET', 'POST'])
def q_bank():
if request.method == "POST":
...
...
elif "final-survey" in request.form:
...
new_survey = Survey(x, y, z) # Where Survey is a custom class created
return redirect(url_for("course_survey", course=new_survey))

在我的代码中,我没有尝试通过 Flask 或直接通过 HTML 创建自己的图标。

脚本也永远不会中断 - 网页加载正常并且运行正常,但我在每条路线上都得到了这个回溯。

最佳答案

可能是 flask 自动尝试获取一个图标

127.0.0.1 - - [07/Sep/2017 10:42:48] "GET /favicon.ico HTTP/1.1" 200 -

即使你没有网站图标你的

@app.route("/<string:course>") 

捕获它并尝试获取 session ["favicon.ico"] 但失败了

错误可能消失了,因为你的浏览器缓存了 favicon.ico,所以它只加载(请求)一次

关于python - jinja2.exceptions.UndefinedError - 没有属性 'favicon.ico',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com