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 Template

Jekabs Dambergs
Jekabs Dambergs
7,417 Points

Stuck in - Lunch Template challenge, Task 1 of 2

I cannot understand where's the problem in my code. Can someone help also explaining the task and the problem?

Description: "Update templates/today.html to show today's order's date and the order attribute from the order variable. Use strftime on the date with the format %Y-%m-%d."

lunch.py
import datetime

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'


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


@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(),
            date=form.date.data,
            order=form.order.data.strip()
        )
    return render_template('lunch.html', form=form)


@app.route('/today')
@login_required
def today():
    order = models.LunchOrder.select().where(
        models.LunchOrder.date == datetime.date.today() &
        models.LunchOrder.user == g.user._get_current_object()
    ).get()
    return render_template('today.html', order=order)


@app.route('/cancel_order/<int:order_id>')
@login_required
def cancel_order(order_id):
    try:
        order = models.LunchOrder.select().where(
            id=order_id,
            user=g.user._get_current_object()
        ).get()
    except models.DoesNotExist:
        pass
    else:
        order.delete_instance()
    return redirect(url_for('index'))
templates/today.html
{% extends "layout.html" %}

{% block content %}
<h1>Your lunch for today</h1>

<h2><time date-time="{{ post.timestamp }}" class="distime" datetime="
  post.timestamp.strftime('%Y-%m-%d') }}"> {{ post.timestamp }} </h2>
<p><!-- print today's lunch order --></p>
<!-- button to the route for cancel_order with order_id=order.id -->
{% endblock %}

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

I can see you understood they wanted you to modify the template file. Let's build on that.

The Challenge wants you to show the order's date in a particular format (%Y-%m-%d) and the order attribute from the order variable.

To see the order's date in the template you'd have to use

{{ order.date }}

but you need to format it too-- so they hint you need to use strftime

{{ order.date.strftime('%Y-%m-%d') }}

and then to show the order attribute of order, that is simply

{{ order.order }}

If you change your <h2> tag inner html as below and your <p> tag inner html like below, you will have a solution.

<h2> {{ order.date.strftime('%Y-%m-%d') }} </h2>
<p> {{ order.order }} </p>
Jekabs Dambergs
Jekabs Dambergs
7,417 Points

Thanks Jeff, when you explain it like that, it seems obvious. :)

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

You're welcome. This course really pulls together quite a lot of important concepts into a single design.

HINT - the project you develop in this course would make a nice project to show as a portfolio piece.

Since it is a full-stack project, you can make it a show-and-tell project for potential clients or prospective employers.

  • social network applications are always interesting to clients
  • demonstrates a full-stack design concept
  • user authentication (and some best practices with hashing/encrypting user logins)
  • demonstrates familiarity with relational database concepts such as join operations
  • it looks nice, but you could make it look even nicer with a fresh HTML/CSS design
  • tests - no software is really complete until you have a test suite with reasonably full coverage.