I'm working on pset 8 and I have a two part question. First, I've tried to implement the articles function as instructed in the mashup pset documentation but I get a builtins.TypeError TypeError: 'list' object is not callable when trying to use the example urls such as: https://ide50-username.cs50.io/articles?geo=02138.
This is my code:
@app.route("/articles", methods=["GET", "POST"])
def articles():
"""Look up articles for geo."""
if request.method == "GET":
if not request.args.get("geo"):
raise RuntimeError("Need geo argument")
else:
request.args.get("geo")
articles_location = lookup("geo")
return articles_location
return jsonify([articles_location])
My second question is about the search function. This function actually kind of works. I can use the sample urls if they include only one search parameter such as https://ide50-username.cs50.io/search?q=02138 but when I try to search for multiple things at the same time (ex. Cambridge+Massachusetts) I just get an empty JSON back. I think I need to update my select from statement using AND and OR but I don't know how to include all the ANDs and ORs I think I might need without cancelling each other out. For example wouldn't having: SELECT * FROM places WHERE postal_code LIKE :q AND place_name LIKE :q OR place_name LIKE :q result in the search results only being returned where postal_code and place_name are similar to q. I'm not sure and get confused by the wording. Anyway here is the code for my search function:
@app.route("/search")
def search():
"""Search for places that match query."""
q = request.args.get("q") + "%"
search_location = db.execute("SELECT * FROM places WHERE postal_code LIKE :q OR place_name LIKE :q OR admin_name1 LIKE :q", q=q)
return jsonify([search_location])
Thanks in advance!
EDIT: So I edited articles but now I get a ValueError: View function did not return a response. I think I did what you were saying but now I'm not sure what I should do to fix this. I also tried out your suggestion for my search function but it would only work when searching for multiple things at once and even then having multiple search parameters wouldn't narrow down the search responses like it should have. I decided to go with my original code for that part and just to be satisfied that something I wrote actually sort of works for once. I do appreciate your help though. Here is the articles code:
@app.route("/articles", methods=["GET", "POST"])
def articles():
"""Look up articles for geo."""
if request.method == "GET":
if not request.args.get("q"):
raise RuntimeError("Need geo argument")
else:
geo = request.args.get("q") + "%"
articles_location = lookup(request.args.get("geo"))
return jsonify([articles_location])