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 Build a Social Network with Flask Tacocat Challenge The Challenge

Yosef Fastow
Yosef Fastow
18,526 Points

getting a "UndefinedError: 'tacos' is undefined" in the tacocat challenge.

In the tacocat challenge any time I try to load the main page I get a Undefined Error: tacos is undefined. I'm not 100% done but its hard to go further because its hard to check progress if you can't see what the home page looks like. I'm not exactly sure what variable tacos represents.I guessed it represents the Taco class in the login. I attached the code below.

(Also the login, logout, register and post pages work.)

tacocat.py

@app.route('/')
def index():
    tacos = models.Taco.select().limit(100)
    return render_template('index.html', index=index)


@app.route('/new-taco', methods=('GET', 'POST'))
@login_required
def tacos():
    form = forms.TacoForm()
    if form.validate_on_submit():
        models.Taco.create(user=g.user._get_current_object(),
                            protein=form.protein.data.strip(),
                            shell=form.shell.data.strip(),
                            cheese=form.cheese.data.strip(),
                            extra=form.extra.data.strip())
        return redirect(url_for('index'))
    return render_template('taco.html', form=form)

forms.py

    class TacoForm(Form):
    protein = StringField("What type of protein is in your taco? Beans? Beef?", validators=[DataRequired()])
    shell = StringField("What type of shell is your taco?", validators=[DataRequired()])
    cheese = StringField("Does it have cheese?", validators=[
            DataRequired()
        ])
    extras = TextAreaField("Add any other detail.", validators=[DataRequired()])

models.py

class Taco(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        rel_model=User,
        related_name='posts'
    )
    protein = CharField()
    shell = CharField()
    cheese = CharField()
    extras = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)

index.html

{% extends 'layout.html' %}

{% block content %}
<h2>Tacos</h2>
    {% if tacos.count() %}
        <table class="u-full-width">
          <thead>
            <tr>
              <th>Protein</th>
              <th>Cheese?</th>
              <th>Shell</th>
              <th>Extras</th>
            </tr>
          </thead>
          <tbody>
        {% for taco in tacos %}
            <tr>
              <!-- taco attributes here -->
                {{ taco.protein }}
                {{ taco.cheese }}
                {{ taco.shells }}
                {{ taco.extras }}
            </tr>
        {% endfor %}
          </tbody>
        </table>
    {% else %}
        <!-- message for missing tacos -->
        <p>Sorry the site is new and empty pleases add and help us start our site!</p>
    {% endif %}
{% endblock %}
Yosef Fastow
Yosef Fastow
18,526 Points

the code you gave seemed to solve the problem, but now I'm getting peewee.OperationalError

peewee.OperationalError: no such column: t1.username

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

"No such column" errors are typically due to a new difference between the models and the database. if you changed the models recently, then you'll need to remake your database.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,167 Points

Can you be very specific to what you are doing when you get be error and can you provide the detailed error message you are seeing?

In tacocat.py above the indentation if the index view is off. It needs to be unindented.

Also in the index view, the value of tacos is defined but not passed to the rendered template. If the template accesses {{ tacos }} it might raise the error.

@app.route('/')
def index():
    tacos = models.Taco.select().limit(100)
    return render_template('index.html', index=index, tacos=tacos)

Can you post your index.html?