python - trouble querying an SQL database held on a Flask app - returning "None" in Flask when it returns data when I interact with the database -
i'm working in flask app, , i'm trying set create dynamic webpages based on data in sql database. example, if scrape data criminal, want flask route requests such can type:
myflaskapp.com/criminal/[criminal's name]/
and taken page criminal. relevant portion of views.py
i've written:
@app.route('/criminal/<first_name>') def criminal(first_name): criminal = individual_criminal.query.filter_by(first_name=first_name).first() return render_template('user.html', criminal=criminal)
now, when call individual_criminal.query.filter_by(first_name=first_name).first()
in python shell, returns expected:
however, when set flask server, , (what believe be) exact same command query, gives me blank page (with navbar , stuff extended base html.)
the html page i'm trying call simple:
<!-- extend base layout --> {% extends "base.html" %} {% block content %} <h1>{{ criminal.full_name }}</h1> <hr> {% endblock %}
as can see, should returning particular criminal's full name (in case, bryan sanford). instead, returns this:
instead of requested criminal's full name, way html specifies.
where going wrong here? thinking if can exact query that's in views.py
file , have return correct value, should work same in flask app. however, there wires crossed somewhere. can of wonderful people me untangle this?
edit: discussed in 1 of answers comments, when change views.py
include print(criminal.first_name)
, fails, throwing attributeerror: 'nonetype' object has no attribute 'first_name'
. though exact same line works expected in actual database!
your routing seems wrong?
this not same
myflaskapp.com/[criminal's name]/
as
@app.route('/criminal/<first_name>')
try
myflaskapp.com/criminal/[criminal's name]/
Comments
Post a Comment