Flask:使用Jinja(user_data)的查询结果未显示在网页上 [英] Flask: Results of the query is not being displayed on the webpage using jinja (user_data)

查看:58
本文介绍了Flask:使用Jinja(user_data)的查询结果未显示在网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个相当基本的烧瓶(使用崇高文字进行编码).为了进行测试,我现在尝试运行以下.py文件.它包含一个生成数据的查询: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 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)

使用IDLE编辑并运行文件bigquestions.py时,出现以下错误:

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'

我通过在正确的python版本中打开它来解决了这个问题. IDLE与venv中安装的ID不同.现在,它可以正确打印查询结果.

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.

仍然无法解决的问题:

  1. 我在某处的设置或代码中犯了一个错误吗? user_data没有显示在网页上.

在home.html中(在模板中)

in the home.html (in templates)

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

...但是网页上未显示user_data.上面的数据样本文本可以很好地工作,因此与呈现无关.

...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.

服务器未返回任何错误

推荐答案

第1部分已解决:如上所述.我在IDLE中打开它时出现了导入错误,默认为Python 3.7,而venv正在运行3.8.文件现在可以正常运行,但第2部分-查询user_data的结果仍未打印.我将接受任何解释此问题/指出错误的答案

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

第2部分:奇怪...我想通了,但不确切知道它为什么起作用.实际上,我必须注释掉其上方的另一个函数,该函数还将参数传递给home.html页面.不允许具有多个传递参数的功能吗?

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?

函数def send_comments()已被注释掉.一切都很好!

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

带有注释功能的代码

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)

这篇关于Flask:使用Jinja(user_data)的查询结果未显示在网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆