0

I'm working in a Flask app, and I'm trying to set it up to create dynamic webpages based on the data in the SQL database. For example, if we scrape data about a certain criminal, I want Flask to route my requests such that I can type:

myflaskapp.com/criminal/[criminal's name]/

and be taken to a page specifically for that criminal. This is the relevant portion of the views.py that I've already 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 I call the Individual_Criminal.query.filter_by(first_name=first_name).first() in a Python shell, it returns as expected: example

However, when I set up my Flask server, and do (what I believe to be) the exact same command query, it just gives me a blank page (with my navbar and stuff extended from the base html.)

The HTML for the page I'm trying to call is simple:

<!-- extend base layout -->
{% extends "base.html" %}

{% block content %}
  <h1>{{ criminal.full_name }}</h1>
  <hr>
{% endblock %}

As you can see, it should be returning the particular criminal's full name (in this case, Bryan Sanford). Instead, it returns this: example2

Instead of the requested criminal's full name, the way that the HTML specifies.

Where am I going wrong here? My thinking is that if I can do that exact query that's in my views.py file and have it return the correct value, it should work the same in my Flask app. However, clearly there are some wires crossed somewhere. Can any of you wonderful people help me untangle this?

edit: as discussed in one of the answers comments, when I change views.py to include print(criminal.first_name), it fails, throwing AttributeError: 'NoneType' object has no attribute 'first_name'. Even though the exact same line works exactly as expected in the actual database! enter image description here

1 Answer 1

1

Your routing seems to be wrong?

This is not the same

myflaskapp.com/[criminal's name]/

as

@app.route('/criminal/<first_name>')

Try

myflaskapp.com/criminal/[criminal's name]/
Sign up to request clarification or add additional context in comments.

8 Comments

ah, would love for it to be that easy a fix, but that was a mistake I made in formatting. Fixed now. Thanks though!
Ok. Have you tried to print your first_name variable within the criminal-function? To see if the argument is correct. On the console you should see the name.
hm. that throws AttributeError: 'NoneType' object has no attribute 'first_name'. why would it return NoneType in the flask app but not in the actual SQL db..?
Can you share your complete output? And what you've tested now.
Is it possible that you have an upper-/lowercase mismatch?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.