3

I'm trying to run my flask application - which was working fine last night. However, this morning, attempting to run the same code (on a different computer, however), I'm getting the error:

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

Full traceback:

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 (relevant code)

@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))

Nowhere in my code am I attempting to create my own favicon, either through flask or through the HTML directly.

The script also never breaks - the webpages load fine and function normally, but I'm getting this traceback on every route.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • Surprisingly, the error has mysteriously gone away - but I would still love an answer with any explanation – TerryA Sep 06 '17 at 04:25
  • Not sure, but see if you would like to add this for next time ? http://flask.pocoo.org/docs/0.12/patterns/favicon/ – Tarun Lalwani Sep 06 '17 at 06:13
  • @TarunLalwani Thanks for the link - I'm sure a favicon is something I'd like to implement in the future as a small little thing :) – TerryA Sep 06 '17 at 06:55

1 Answers1

1

It might be flask automatically tries to get a favicon

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

and even though you don't have a favicon your

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

catches that and tries to grab sessions["favicon.ico"] and fails

The error probably went away because your browser caches favicon.ico so it only loads (requests) it once

Friedpanseller
  • 654
  • 3
  • 16
  • 31