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

Not really getting whats asked here. Broadcasting lunch order form

I know I'm being asked for an if statement and then lunchorderform be sent to the template. but what am I missing?

forms.py
from flask_wtf import Form
from wtforms import StringField, PasswordField, TextAreaField, DateField
from wtforms.validators import DataRequired, Email, Length


class SignUpInForm(Form):
    email = StringField(validators=[DataRequired(), Email()])
    password = PasswordField(validators=[DataRequired(), Length(min=8)])

class LunchOrderForm(Form):
  order = TextAreaField(
    validators=[
      DataRequired()
    ]
  )

  date = DateField(
    validators=[
      DataRequired()
    ]
  )
lunch.py
from flask import Flask, g, render_template, flash, redirect, url_for
from flask.ext.bcrypt import check_password_hash
from flask.ext.login import LoginManager, login_user, current_user, login_required, logout_user

import forms
import models

app = Flask(__name__)
app.secret_key = 'this is our super secret key. do not share it with anyone!'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

@app.route('/order', methods=('GET', 'POST'))
def order_lunch():
  form = forms.LunchOrderForm()
  if form.validate_on_submit():
    models.User.create_user(
      email=form.email.data,
            password=form.password.data
    )
  return render_template('lunch.html', form=form)


@login_manager.user_loader
def load_user(userid):
    try:
        return models.User.select().where(
            models.User.id == int(userid)
        ).get()
    except models.DoesNotExist:
        return None


@app.before_request
def before_request():
    g.db = models.DATABASE
    g.db.connect()
    g.user = current_user


@app.after_request
def after_request(response):
    g.db.close()
    return response


@app.route('/register', methods=('GET', 'POST'))
def register():
    form = forms.SignUpInForm()
    if form.validate_on_submit():
        models.User.new(
            email=form.email.data,
            password=form.password.data
        )
        flash("Thanks for registering!") 
    return render_template('register.html', form=form)


@app.route('/login', methods=('GET', 'POST'))
def login():
    form = forms.SignUpInForm()
    if form.validate_on_submit():
        try:
            user = models.User.get(
                models.User.email == form.email.data
            )
            if check_password_hash(user.password, form.password.data):
                login_user(user)
                flash("You're now logged in!")
            else:
                flash("No user with that email/password combo")
        except models.DoesNotExist:
              flash("No user with that email/password combo")
    return render_template('register.html', form=form)

@app.route('/secret')
@login_required
def secret():
    return "I should only be visible to logged-in users"

@app.route('/logout')
def logout():
    logout_user()
    return redirect(url_for('login'))


@app.route('/')
def index():
    return render_template('index.html')

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Aboubacar.

I came across your question a few hours ago but I hadn't done the challenge, so let's see if I can help you now that I have done it:

Let's start with your code:

@app.route('/order', methods=('GET', 'POST'))
def order_lunch():
  form = forms.LunchOrderForm()
  if form.validate_on_submit():
    models.User.create_user(
      email=form.email.data,
            password=form.password.data
    )
  return render_template('lunch.html', form=form)

I think it is good till the if statement (included), then come the troubles.

  1. We do NOT want to create a new User, but instead we want a new LunchOrder instance.

  2. The LunchOrder instance will not want email and password as fields, but will want these instead: user, date and order and they will need to be filled in accordingly to the form fields. I recommend you double check the last video for the current user value, while the other 2 value can be retrieved from the form as we did in previous challenges.

The rest seems ok to me, if you have other questions please ask.

Vittorio

Ah thank you, I realized my mistake, I thought it was asking for a new user within the lunch order form, gonna work towards it now. thanks!