displaying information from two tables in one page Flask

时间:2016-10-20 18:43:34

标签: python flask flask-sqlalchemy

I am working with a flask application and i am running into something that I have never really seen before. I want to output information about a restaurant from a database, so i created a for loop that will go though and output each of the saved restaurants name in one table and other information in a different table.

As i am going to display them in my html document only the information from the first table is being displayed and eveerything from the second table is not being displayed for some reason.

I know that the loop goes through the restaurants table, which only stores the restaurant name and what i cant figure out is how to get the rest of the information that is saved to be displayed from the other table...

here is the html document

<html>
  <head>

  </head>
  <body>
    <h1>Here are all of the current restaurants available</h1>
    <br>

    {% with alerts = get_flashed_messages() %}
      {% if alerts %}
        <ul>
          {% for alert in alerts %}
          <li>
            {{alert}}
          </li>
          {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}

    <br>
    <br>

    <a href="{{url_for('CreateRestaurant')}}">Add a new restaurant</a>

    <br>
    <br>

    {% for restaurant in restaurants %}
      Name: {{restaurant.name}}
      <br>
      Food Type: {{restaurant.food_type}}
      <br>
      Prices: {{restaurant.price}}
      <br>
      Location: {{restaurant.city}}, {{restaurant.state}}
      <br><br>
    {% endfor %}

  </body>
</html>

here is the flask route connected to it:

@app.route('/addrestaurant', methods=['GET', 'POST'])
def CreateRestaurant():
    if request.method == 'POST':
        newRestaurant = Restaurants(name = request.form['name'])
        session.add(newRestaurant)
        session.commit()
        newRestaurantInfo = Restaurants_Info(food_type = request.form['food_type'],
                                             avg_price = request.form['avg_price'],
                                             city = request.form['city'],
                                             state = request.form['state'],
                                             restaurants_id = newRestaurant.id)
        session.add(newRestaurantInfo)
        session.commit()
        flash('%s was successfully created' % newRestaurant.name)
        return redirect(url_for('DisplayRestaurant'))
    else:
        return render_template('addRestaurant.html')

@app.route('/restaurants')
def DisplayRestaurant():
    restaurants = session.query(Restaurants).order_by(Restaurants.name)
    return render_template('displayRestaurants.html', restaurants = restaurants)

Here are the models:

class Restaurants(Base):
    __tablename__ = 'restaurants'

    id = Column(Integer, primary_key = True)
    name = Column(String(100), nullable = False, unique = True, index = True)

# the following class will store all of the restaurants general informaiton
class Restaurants_Info(Base):
    __tablename__ = 'restaurants_info'

    id = Column(Integer, primary_key = True)
    food_type = Column(String(100), nullable = False)
    avg_price = Column(Integer(5), nullable = False)
    city = Column(String(100), nullable = False, index = True)
    state = Column(String(3), nullable = False)
    restaurants_id = Column(Integer(100), ForeignKey('restaurants.id'))
    restaurants = relationship(Restaurants)

0 个答案:

没有答案