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,
}
]
}