databaseScrnshotThis is part of my code that should return info from database. Query is not returning anything—not even an error. My code is as follows from my application.py. I managed to get my website on book review to load, authenticate, and make a sign in and signout. Now the important part of returning book queries doesn't seem to call anything to the screen. I am suspecting it could be my submit button, but I have not been lucky with picking out the cause.
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == "POST":
title = request.form['byTitle']
author = request.form['byAuthor']
year = request.form['byYear']
isbn = request.form['byIsbn']
list = []
text = None
baseUrl = request.base_url
if title:
result = db.execute(" SELECT * FROM books WHERE title LIKE '%"+title+"%' ;").fetchall()
text = title
elif author:
result = db.execute(" SELECT * FROM books WHERE author LIKE '%"+author+"%' ;").fetchall()
text = author
elif year:
result = db.execute(" SELECT * FROM books WHERE year = :year", {'year':year}).fetchall()
text = year
else:
result = db.execute(" SELECT * FROM books WHERE isbn LIKE '%"+isbn+"%' ;").fetchall()
text = isbn
#save it in list
if result:
for i in result :
list.append(i)
itemsCount = len(list)
return render_template('search.html', baseUrl = baseUrl, items = list, msg = "Search result found", text = text , itemsCount = itemsCount)
#Not found message
else:
return render_template('search.html', msgNo = "No books found" , text = text)
More info from top of my application.py
from flask import Flask, render_template, redirect, url_for, jsonify, request
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import InputRequired, Email, Length
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import Users, requests, psycopg2
app = Flask(__name__)
app.config['SECRET_KEY'] = 'iaddedit'
app.config['SQLALCHEMY_DATABASE_URI'] = 'this is added too'
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
here's some code from 'search.html'
<div class="col-sm-12 col-md-9">
{% if itemsCount %}
<div
class="text-center alert alert-success alert-dismissible fade show"
role="alert"
>
<button
type="button"
class="close"
data-dismiss="alert"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
<p>
Total <strong> {{itemsCount}} </strong> result found for:
<strong> {{text}}</strong>
</p>
</div>
<script>
$(".alert").alert();
</script>
{%endif%} {% if msgNo %}
<div
class="text-center alert alert-danger alert-dismissible fade show"
role="alert"
>
<button
type="button"
class="close"
data-dismiss="alert"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
<p>{{msgNo}} for: <strong> {{text}}</strong></p>
</div>
<script>
$(".alert").alert();
</script>
{{hello}} {%endif%} {%if items%}
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Year</th>
<th>ISBN</th>
</tr>
</thead>
<tbody>
{% for i in items %}
<tr>
<td><a href="{{baseUrl}}/{{i[1]}}"> {{i[2]}}</a></td>
<td>{{i[3]}}</td>
<td>{{i[4]}}</td>
<td><a href="{{baseUrl}}/api/{{i[1]}}"> {{i[1]}}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{%endif %}
</div>
</div>
{%endblock%}