Skip to content Skip to sidebar Skip to footer

Flask: Results Of The Query Is Not Being Displayed On The Webpage Using Jinja (user_data)

I have a fairly basic flask set up (been coding using sublime text). For testing purposes, I am now trying to run the following .py file. It contains a query that generates the dat

Solution 1:

try this code below

[..]
defquery_comments():
    connie = sqlite3.connect(
        db_locale,
        detect_types=sqlite3.PARSE_DECLTYPES
    )
    user_data = connie.execute("""SELECT * FROM comments""").fetchall()
    connie.close()
    return user_data

[..]

refer to official Flask tutorial topic Define and Access the Database

the github repo

it seems you solved the part one but it looks like a hack not the right way.

it seems you have 2 versions of python on your windows system so you have to set the right environment system to avoid mixing between python versions and virtual environments

Solution 2:

Part 1 solved: As updated above. It was coming up with the import error as I was opening it in IDLE which was defaulting to Python 3.7 and the venv was running 3.8. The files now run fine BUT Part 2 - the results of the query user_data are still not being printed. I'll accept any answer that explains this/points out the error

Part 2: Odd...I figured it out, but don't understand exactly why it worked. I essentially had to comment out another function above it that was also passing parameters to the home.html page. Is it not allowed to have multiple functions that pass parameters?

The function def send_comments() was commented out. All worked fine then!

Code with commented out function

from datetime import datetime
from flask import Flask,render_template,url_for

import sqlite3

app = Flask(__name__) 
db_locale='users.db'"""

@app.route('/home')
def sendcomments():
    sampletext="Responses:" #sending this as a variable to home
    return render_template('home.html',sampletext=sampletext)
"""@app.route('/') @app.route('/home')defhome():
    user_data=query_comments() #new variable user_Data that is the result from this queryreturn render_template('home.html',user_data=user_data)


@app.route('/about') defabout():
    return render_template('about.html')
"""
@app.route('/addcomment')
def addcomment():
    return render_template()
"""defquery_comments():
        connie = sqlite3.connect(db_locale)
        c=connie.cursor()
        c.execute("""
        SELECT * FROM comments  

        """)
        userdata=c.fetchall()
        return userdata


query_comments()

if __name__ == '__main__':
    app.run(debug=True)

Post a Comment for "Flask: Results Of The Query Is Not Being Displayed On The Webpage Using Jinja (user_data)"