Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask Basics Character Builder Cookies

Brian Patterson
Brian Patterson
19,588 Points


I have the following code in my app.py file.

import json
from flask import (Flask, render_template, redirect,
            url_for, request, make_response)

app = Flask(__name__)

def get_saved_data():
        data = json.loads(request.cookies.get('character'))
    except TypeError:
        data = {}
    return data

def index():
    data = get_saved_data
    return render_template('index.html', saves=data)

@app.route('/save', methods=['POST'])
def save():
    response = make_response(redirect(url_for('index')))
    data = get_saved_data() #You have this information already stored
    data.update(dict(request.form.items()))#Update that information
    response.set_cookie('character', json.dumps(data))# the send it back.
    return response

if __name__ == '__main__':

Now in my index file I have the following:

{% extends "layout.html" %}
{% block content %}
<!--Enter Name -->
<div class="enter-name">
    <div class="grid-100">
        <img src="/static/img/bear_avatar.svg" class="bear-avatar" />
    <div class="grid-100">
        <form action="{{ url_for('save') }}" method="POST">
            <label>Name your bear</label>
            <input type="text" name="name" value="{{ saves.get('name','')}}" autofocus>
            <input type="submit" value="Let's build it!">
{% endblock %}

The problem I am getting is when I save the file I get the following error:

jinja2.exceptions.UndefinedError: 'function object' has no attribute 'get'

[MOD: added ```python formatting -cf]

1 Answer

Adam Cameron
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

Oh. I figured it out. In your index() view you need to add a pair of parentheses. The language to describe exactly why this isn't working isn't coming to me but I know it has to do with calling a function object versus the output of a function itself. Maybe our buds Kenneth Love and Chris Freeman can further explain.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,995 Points

You've got it! In the statement:

    data = get_saved_data

data is getting assigned the function itself, not the results of the execution of the function get_saved_data. To get the execution, parens must be present.

In Python, functions can be passed around like any other variable. This is especially useful in python functions like map and filter that take a function as the first argument.