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

时间:2017-09-06 00:21:38

标签: python python-3.x flask jinja2

我正在尝试运行我的烧瓶应用程序 - 昨晚工作正常。但是,今天早上,尝试运行相同的代码(但在另一台计算机上),我收到错误:

  

jinja2.exceptions.UndefinedError:'flask.sessions.SecureCookieSession object'没有属性'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))

我的代码中没有任何地方可以通过烧瓶或HTML直接创建自己的图标。

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

1 个答案:

答案 0 :(得分:1)

可能是烧瓶自动尝试获取图标

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

即使你没有自己的图标

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

抓住并尝试抓住会话[&#34; favicon.ico&#34;]并失败

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