Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask Broadcasting Lunch Order Form

Flask LunchOrderForm. Not sure where I'm going wrong...

Hi everyone, I've been stuck on this challenge for over 30 minutes now hoping I could figure it out. Unfortunately, I still have not found my solution. I am on Task 3 and my code for forms.py works but I do not know what I am doing wrong in lunch.py. Here is my code for the view in lunch.py:

@app.route('/order', methods = ('GET', 'POST'))
def order_lunch():
    form = form.LunchOrderForm()
    if form.validate_on_submit():
      models.LunchOrder.new(
        order = form.order.data,
        date = form.date.data,
        user = g.user
        )
    return render_template('lunch.html', form = form)

Any help would be much appreciated :)

2 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

Look back in luch.py and you'll see:

@app.route('/order', methods=('GET', 'POST'))
def order_lunch():
    form = forms.LunchOrderForm()
    if form.validate_on_submit():
        models.LunchOrder.create(user=g.user._get_current_object(), data=form.content.data.strip()) 
        #^here^

    return render_template('lunch.html', form=form)

#^here^ marks a previous line that you're overlooking.

1) You are using .new() rather than .create(). The function .new() is not function in python at all. I assume you're mixing up your python with javascipt. In python you use .create() to create new objects.

2) Your g.user is incomplete. It needs the getter function which is also defined in the same line, otherwise it won't change values upon different input.

Putting all of this together, the correct code should be:

@app.route('/order', methods=('GET', 'POST'))
def order_lunch():
    form = forms.LunchOrderForm()
    if form.validate_on_submit():
        models.LunchOrder.create(
        # ^Add .create() for models.LunchOrder
            user=g.user._get_current_object(),
            # ^Add ._get_current_object() for user
            order=form.order.data,
            date=form.date.data,
            )

    return render_template('lunch.html', form=form)

Let me know if you have anymore troubles and/or questions.

Thanks very much Andrew! I knew that i should have used _get_current_object(). Also, thank you for pointing out that I had to use create, I was completely unaware of that. Also, what does .new() do? Is new() only used when creating new users? Thanks!

Andrew Winkler
Andrew Winkler
37,739 Points

If you look back in models.py, you'll see that .new() is just a general function we defined. It is not part of the python library. Ergo .new() does whatever we assign it to do. In this context we have assigned .new() as a classmethod which entails .new() only applies to the designated class 'User'. In context .new() assigns a hashed password to the password field.

class User(UserMixin, Model):
    email = CharField(unique=True)
    password = CharField(max_length=100)
    join_date = DateTimeField(default=datetime.datetime.now)
    bio = CharField(default='')

    class Meta:
        database = DATABASE

    @classmethod
    def new(cls, email, password):
   # ^*here*^
        cls.create(
            email=email,
            password=generate_password_hash(password)
        )

Flask and this course has a lot to do with how databases work. You may wanna play around with it more to figure out the nooks and crannies.

Thanks again! I completely forgot that new was a function that we defined and is not pre-built.