I am building a web application in Python Flask where the user can create an account and select one or more tourist attractions in the city of Boston. Here is the HTML (the user can select one or more attractions from categories “start”, “waypoints”, and “end”):
<select id="start" name="start">
<option value="Arnold Arboretum, Boston, Massachusetts">Arnold Arboretum</option>
<option value="Beacon Hill, Boston, Massachusetts">Beacon Hill</option>
<option value="Boston Public Garden">Boston Public Garden</option>
<option value="Boston Public Library">Boston Public Library</option>
<option value="Boston Public Garden">Boston Public Garden</option>
<option value="Boston Public Library">Boston Public Library</option>
<option value="Chinatown, Boston">Chinatown</option>
<option value="Fanueil Hall, Boston">Fanueil Hall</option>
<option value="Fenway Park, Boston">Fenway Park</option>
<option value="Isabella Steward Gardner Museum, Boston">Isabella Steward Gardner Museum</option>
<option value="Museum of Fine Arts, Boston, Massachusetts">Museum of Fine Arts</option>
<option value="Museum of Science, Boston, Massachusetts">Museum of Science</option>
<option value="New England Aquarium">New England Aquarium</option>
<option value="North End, Boston">North End</option>
<option value="TD Garden, Boston">TD Garden</option>
<option value="USS Constitution, Boston">USS Constitution</option>
</select>
<select multiple id="waypoints" name="waypoints">
<option value="Arnold Arboretum, Boston, Massachusetts">Arnold Arboretum</option>
<option value="Beacon Hill, Boston, Massachusetts">Beacon Hill</option>
<option value="Boston Public Garden">Boston Public Garden</option>
<option value="Boston Public Library">Boston Public Library</option>
<option value="Bunker Hill Monument, Boston">Bunker Hill Monument</option>
<option value="Chinatown, Boston">Chinatown</option>
<option value="Fanueil Hall, Boston">Fanueil Hall</option>
<option value="Fenway Park, Boston">Fenway Park</option>
<option value="Isabella Steward Gardner Museum, Boston">Isabella Steward Gardner Museum</option>
<option value="Museum of Fine Arts, Boston, Massachusetts">Museum of Fine Arts</option>
<option value="Museum of Science, Boston, Massachusetts">Museum of Science</option>
<option value="New England Aquarium">New England Aquarium</option>
<option value="North End, Boston">North End</option>
<option value="TD Garden, Boston">TD Garden</option>
<option value="USS Constitution, Boston">USS Constitution</option>
</select>
<select id="end" name="end">
<option value="Arnold Arboretum, Boston, Massachusetts">Arnold Arboretum</option>
<option value="Beacon Hill, Boston, Massachusetts">Beacon Hill</option>
<option value="Boston Public Garden">Boston Public Garden</option>
<option value="Boston Public Library">Boston Public Library</option>
<option value="Bunker Hill Monument, Boston">Bunker Hill Monument</option>
<option value="Chinatown, Boston">Chinatown</option>
<option value="Fanueil Hall, Boston">Fanueil Hall</option>
<option value="Fenway Park, Boston">Fenway Park</option>
<option value="Isabella Steward Gardner Museum, Boston">Isabella Steward Gardner Museum</option>
<option value="Museum of Fine Arts, Boston, Massachusetts">Museum of Fine Arts</option>
<option value="Museum of Science, Boston, Massachusetts">Museum of Science</option>
<option value="New England Aquarium">New England Aquarium</option>
<option value="North End, Boston">North End</option>
<option value="TD Garden, Boston">TD Garden</option>
<option value="USS Constitution, Boston">USS Constitution</option>
</select>
And then hit the submit button:
<input type="submit" id="submit" value="Submit"/>
In Flask, I have built a functionality which saves the user’s selected options into a variable that is stored in an SQL database:
@app.route("/", methods=['GET','POST'])
@login_required
def index():
if request.method == "GET":
return render_template("attractions5.html")
else:
start = (request.form.get['start'])
waypoints = (request.form.get['waypoints'])
end = (request.form.get['end'])
route = Route(start=start, waypoints=waypoints, end=end)
db.execute("INSERT INTO routes (userid, route) VALUES (?, ?)", userid=session["user_id"], route=route)
return redirect("/")
From here, I want to be able to print the stored values back into the HTML webpage, such that if the user were to login to their account at a later time, their selections would be maintained. However, I don’t know how to implement this functionality into Flask. I’m also not sure if my current implementation in Flask is correct as far as saving the user’s selections. Let me know if I should clarify anything else – thanks in advance!