I am Bob, a part-time teacher. I've always found it difficult to remember my class schedules. And as I was learning Flask I thought of building a scheduling API that will display the schedule for a specific day.
I managed to write the following code using the /schedule/ endpoint that shows the correct response (the days when I have classes), but when I add a specific day, for example, /schedule/Monday, it shows me the 404 Not Found error. Help me please.
1 from flask import Flask , jsonify
2 from flask.views import MethodView
3
4
5 app = Flask(__name__)
6
7 schedules = [{"Day":"Monday","schedule":["Math","Physics"]},
8 {"Day":"Tuesday","schedule":["Math","Cs"]},
9 {"Day":"Thursday","schedule":["Physics","Cs"]}]
10
11 class Schedule(MethodView):
12 def get(self,day):
13 if not day:
14 return jsonify({"Days":[schedule["Day"] for schedule in schedules]})
15 else:
16 for schedule in schedules:
17 if schedule["Day"] == day:
18 return jsonify(schedule)
19 return jsonify({"Message":"Invalid Subject"})
20 schedule_view = Schedule.as_view("schedule_api")
21 app.add_url_rule("/schedule",view_func = schedule_view ,defaults = {"day":None})
22
23
24
25 app.run(debug = True)
Which of the following will help Bob to get rid of the error in the code above?