0

Here is my code :

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem

app = Flask(__name__)

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()


@app.route('/')
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id)
    output = ''
    for i in items:
        output += i.name
        output += '</br>'
        output += i.price
        output += '</br>'
        output += i.description
        output += '</br>'
        output += '</br>'
    return output

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

I run the file by entering the following URL :

localhost:5000/restaurants/2

And all I get is a blank page. I don't get any kind of error in my GitBash or browser or anywhere else.

I ran this simple code :

from flask import Flask
app = Flask(__name__)


@app.route('/')
@app.route('/hello')
def HelloWorld():
    return "Hello World"

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

And this runs perfectly. What wrong am I doing ?

EDIT : 1

Tried to debug the code and noticed that the code doesn't enter the for i in items: loop.

@app.route('/')
@app.route('/hello')
def restaurantMenu():
    restaurant = session.query(Restaurant).first()
    items = session.query(MenuItem).filter_by(restaurant_id = restaurant.id)
    output = ''
    print restaurant.name
    print restaurant.id

    if not items :
        print "No menu items!"
    else:
        print "There are menu items!"
        for i in items:
            print "In items loop!"
            output += i.name
            output += '<br>'
    return output + "Hello"

enter image description here

13
  • 1
    Have you setup your database so that there are actually records returned? As your code is written, an empty response is a perfectly valid response if no records are retrieved. Commented Jan 30, 2016 at 6:01
  • I have records in the database! Is there someway to debug ? Commented Jan 30, 2016 at 6:02
  • @sholsapp : Please see the screen capture i have added of the gitbash.. for what am i getting a 404 ? Commented Jan 30, 2016 at 6:09
  • ignore that 404 its just a favicon icon Commented Jan 30, 2016 at 6:26
  • @Ja8zyjits : Just realised! Commented Jan 30, 2016 at 6:29

1 Answer 1

1

Basically you need the all() function at your items. You are querying properly but you are not fetching the results.

so your code will be like this

@app.route('/')
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    items = session.query(MenuItem).filter_by(restaurant_id=restaurant.id).all()
    output = ''
    for i in items:
        output += i.name
        output += '</br>'
        output += i.price
        output += '</br>'
        output += i.description
        output += '</br>'
        output += '</br>'
    return output

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=5000)

Now it will depend on the entries in your db, if you have any or else it will return an empty []. So if you have the results, you can print and check or loop and render it to your templates.

This should fix your issue

Sign up to request clarification or add additional context in comments.

6 Comments

Yes that did the trick. But suddenly i realize that the list is empty! I would be grateful if you see this code. I used this script to insert menu items : pastebin.com/e2XUruJi
@AbhishekGhosh I shall look into it, but that will be another question, right? Please accept this if it solves the problem for which you posted initially. :)
I accepted your answer! If you could now just look into the script... I will ask another question then!
@AbhishekGhosh that will be the proper way to do it in here. One small advise use session.commit() at the end of all the add and if possible use a csv, that shall be a better way to do it instead of writing all entries.
I am pursuing an online course from Udacity. They had provided me with this file! That is why I used it. Anyways, thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.