-1

Learning Flask and I'm stuck trying to create a for loop that iterates over each element in the list of cars. I need to concatenate this to html so that if I navigate to each page for the <car_type>s, I can see the lists of cars and their features. This may be really simple but somehow I cant seem to make it work.

I want it to look like this; If I go to the pickups page link <.../cars/pickups> List of Pickups

  • Color: green
  • Mileage: 23000
  • Color: silver
  • Mileage: 40000

If I go to the Sedans page link <.../cars/sedans> List of Sedans

  • color: black
  • mileage: 10000
from flask import Flask
app = Flask(__name__)
from dict import cars

@app.route('/')
def index():
  return f'''
  <h1>Buy a Car!</h1>
  <p>Browse through the links below to find your new mean machine:</p>
  <ul>
  <li>Pickups</li>
  <li>Sedans</li>
  <li>Trucks</li>
  </ul>
  '''

@app.route('/cars/<car_type>')
def car_list(car_type):
  html = f'<h1>List of {car_type}</h1>'
  for car_type in cars.keys():
    html += cars[Pickups], cars[Sedans],cars[Trucks]
  return html  
    

Here is the code for the dictionary in a different python file(dict.py):

cars = {
    'Pickups': [
        {
            'color': 'green',
            'mileage': 23000,
        },
        {
            'color': 'silver',
            'mileage': 40000,
             }
    ],
    'Sedans': [
        {
            'color': 'black',
            'mileage': 10000,
        }
    ],
    'Trucks': [
        {
            'colors': 'sky blue',
            'mileage': 43000,
        }
    ]
}
evacoder
  • 11
  • 5
  • can you show an example of the expected result or what you are trying to achieve? – Isaac Rivera Jan 02 '21 at 13:40
  • 1
    Your app needs to be built from scratch. A lot of mistakes. You must need to create a jinja loop inside your html file in order to get what you want. See here : https://stackoverflow.com/questions/29706099/how-to-make-a-for-loop-in-jinja Also, in your cars route,
  • tags must include links ( tags) to cars/types.
  • – IoaTzimas Jan 02 '21 at 13:58