I am currently working on my cs50 final project. I used the shows search function from week 9 source code as a reference to create my own search function. The search function is called using a form in the navigation bar in layout.html. The search function is not giving any error message in the terminal rather, it is showing the error page on the web application, everything seems fine on the terminal. The search function is similar to the index function and the index function works. I checked the sql query and it gives the appropriate results. Can someone identify the error in the code?
Reference: https://cdn.cs50.net/2020/fall/lectures/9/src9.pdf- shows0/application.py
application.py
app = Flask(__name__)
@app.errorhandler(Exception)
def handle_exc(e):
if not isinstance(e, HTTPException):
e = InternalServerError()
return render_template("error.html", message=e.name, code=e.code)
db = SQL("sqlite:///ideahub.db")
@app.route("/")
def index():
ideas = db.execute("SELECT ideas.id AS id, username, user_id, idea_name, idea_body, post_date FROM ideas INNER JOIN users ON users.id = ideas.user_id WHERE posted = 1 ORDER BY post_date DESC")
for idea in ideas:
if idea["id"] in [row["idea_id"] for row in db.execute("SELECT idea_id FROM likes WHERE user_id = ?", session.get("user_id", 0))]:
idea["liked"] = True
return render_template("index.html", ideas=ideas)
@app.route("/search")
def search():
query = request.args.get("q")
ideas = db.execute("SELECT ideas.id AS id, username, user_id, idea_name, idea_body, post_date FROM ideas INNER JOIN users ON users.id = ideas.user_id WHERE posted = 1 AND idea_name LIKE '%?%' ORDER BY post_date DESC", query)
for idea in ideas:
if idea["id"] in [row["idea_id"] for row in db.execute("SELECT idea_id FROM likes WHERE user_id = ?", session.get("user_id", 0))]:
idea["liked"] = True
return render_template("search.html", ideas=ideas)
search.html
{% extends "layout.html" %}
{% block title %}
Search Results
{% endblock %}
{% block main %}
{% for i in range(ideas|length) %}
<div class="card bg-white col-10 mx-auto mb-2">
<div class="card-body">
<h5 class="card-title font-weight-bold" role="button" data-toggle="collapse" data-target="#idea-content-{{ i }}" aria-expanded="false">
{{ ideas[i].idea_name }} <i class="fas fa-angle-down fa-lg float-right"></i>
</h5>
<div class="collapse" id="idea-content-{{ i }}">
<p class="fw-600 card-text mb-2">Posted:- {{ ideas[i].post_date|date_format }}</p>
<p class="fw-600 card-text mb-2">Created By:- <a class="text-dark hover-link" href="/profile/{{ ideas[i].user_id }}">{{ ideas[i].username }}</a></p>
<p class="card-text">{{ ideas[i].idea_body }}</p>
{% if session.user_id != ideas[i].user_id %}
<form action="/like">
{% if ideas[i].liked %}
<button class="btn btn-primary mr-2" disabled>Liked!</button>
{% else %}
<button type="submit" class="btn btn-primary mr-2" name="id" value="{{ ideas[i].id }}">Like 💛</button>
{% endif %}
</form>
{% endif %}
</div>
</div>
</div>
{% endfor %}
{% endblock %}
layout.html
<ul class="navbar-nav ml-auto mt-2">
<li class="nav-item">
<form class="search-bar bg-dark p-2 mr-2 d-flex justify-content-center align-items-center" action="/search">
<input class="search-input text-white border-0 bg-transparent" autocomplete="off" type="search" name="q" placeholder="Search...">
<button class="text-white deco-none float-right border-0 bg-transparent" type="submit"><i class="fas fa-search"></i></button>
</form>
</li>
</ul>
index.html
{% extends "layout.html" %}
{% block title %}
Homepage
{% endblock %}
{% block main %}
<h3 class="text-white offset-1 mb-3">Homepage</h3>
{% for i in range(ideas|length) %}
<div class="card bg-white col-10 mx-auto mb-2">
<div class="card-body">
<h5 class="card-title font-weight-bold" role="button" data-toggle="collapse" data-target="#idea-content-{{ i }}" aria-expanded="false">
{{ ideas[i].idea_name }} <i class="fas fa-angle-down fa-lg float-right"></i>
</h5>
<div class="collapse" id="idea-content-{{ i }}">
<p class="fw-600 card-text mb-2">Posted:- {{ ideas[i].post_date|date_format }}</p>
<p class="fw-600 card-text mb-2">Created By:- <a class="text-dark hover-link" href="/profile/{{ ideas[i].user_id }}">{{ ideas[i].username }}</a></p>
<p class="card-text">{{ ideas[i].idea_body }}</p>
{% if session.user_id != ideas[i].user_id %}
<form action="/like">
{% if ideas[i].liked %}
<button class="btn btn-primary mr-2" disabled>Liked!</button>
{% else %}
<button type="submit" class="btn btn-primary mr-2" name="id" value="{{ ideas[i].id }}">Like 💛</button>
{% endif %}
</form>
{% endif %}
</div>
</div>
</div>
{% endfor %}
{% endblock %}
This sql gives an error to the effect that there are more parameters than placeholders:
SELECT ideas.id AS id, username, user_id, idea_name, idea_body, post_date
FROM ideas
INNER JOIN users ON users.id = ideas.user_id
WHERE posted = 1 AND idea_name LIKE '%?%'
ORDER BY post_date DESC
This %?%
is a string literal. Either break the ?
out from quotes or use named parameter (which would also not be in quotes). I don't know why you don't see this error in the flask log. That's ostensibly how I found it on repro.