Flask: Results of the query is not being displayed on the webpage using jinja (user_data)

跟風遠走 提交于 2020-07-10 07:40:10

问题


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 data: user_data. I am trying to see what the result of that data is.

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

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

def query_comments():
        connie=sqlite3.connect(db_locale)
        c=connie.cursor()
        c.execute("""
        SELECT * FROM comments  

        """)
        user_data=c.fetchall()
        print(user_data) #CHANGE THIS TO return user_data


@app.route('/') 
@app.route('/home')
def home():
    user_data=query_comments() #new variable user_Data that is the result from this query
    return render_template('home.html',user_data=user_data)

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

On Editing with IDLE and running the file bigquestions.py, it comes up with the following error:

from flask import Flask, render_template, url_for
ModuleNotFoundError: No module named 'flask'

I solved this by opening it in the right python version. IDLE is not the same as what was installed in the venv. It now prints the results of the query correctly.

What still doesn't work:

  1. Have I made a mistake in the set up or code somewhere? The user_data is NOT being displayed on the webpage.

in the home.html (in templates)

<p><b>{{ sampletext }}</b></p>
<p>{{ user_data }}</p>

...but user_data is not being displayed on the webpage. The above data sampletext works fine, so it isn't a matter of rendering.

I think I've just missed a step or some code, and am not sure what.

The server returns NO errors


回答1:


try this code below

[..]
def query_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

  • How to run multiple Python versions on Windows

  • deploying python flask application in apache 24 (my answer on other topic: have a look at the part regarding configuring environment variables)




回答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')
def home():
    user_data=query_comments() #new variable user_Data that is the result from this query
    return render_template('home.html',user_data=user_data)


@app.route('/about') 
def about():
    return render_template('about.html')
"""
@app.route('/addcomment')
def addcomment():
    return render_template()
"""


def query_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)


来源:https://stackoverflow.com/questions/62579373/flask-results-of-the-query-is-not-being-displayed-on-the-webpage-using-jinja-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!