python动态变量引用_使用Flask动态HTML内容和Python变量

On the user end, say I have people selecting school records through a form to display in a table below.

For instance, they choose their name from a list of names, and that pulls their records from a database.

Dependent on their class year, they may have 1, 2, 3, or 4 years of records, so the data pulled will always look different.

After they submit the form, their records are stored in a variable containing all their records, and then each record is broken down into subtypes, for instance, all records in the English department. Those subtypes are stored in other lists--so there's a list for all English records. Let's call that variable english_records. I want to use these subtype variables to be able to present only the data users want to see, and to present all data in that particular list.

So using Flask's render_template function, I'm trying to send each of these records to an html template that will create a table cell for each record.

What I've been trying (that hasn't worked so far) is something like:

Python:

i = 0

def index():

for e in english_records:

english_records = [

{

'english': english_records[i]

}

]

i = i + 1

return render_template("index.html",

english_records = english_records)

And in HTML:

...table above...

{% for record in english_records %}

{{record.english}}

{% endfor %}

...table continues...

Thus far I've been getting table cells created for each record, but the records not being passed through. Anyone know how to do this?

解决方案

Is there a reason why you're pre-processing the data? What does english_records look like? If my hunch is correct, then you shouldn't actually need the for loop at all.

There's also the issue that you're overwriting the initial english_records variable with english_records within the for loop, so the assignment, while legal in terms of syntax, is logically nonsensical.

Another issue is that, depending on the actual type of the first english_records, you shouldn't need to use a counter: if english_records is a list, then it'll contain the value you're looking for. If english_records is a dict, then enumerating it might look like for key, val in english_records.iteritems().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要使用 PythonFlask 框架来实现 Web 应用程序,然后连接到你的数据库。 在 Flask 中,你可以使用 Flask-SQLAlchemy 扩展来连接到常见的 SQL 数据库,如 MySQL、PostgreSQL 和 SQLite。这个扩展提供了一个 SQLALCHEMY_DATABASE_URI 配置选项,你需要设置为指向你的数据库的连接字符串。 下面是一个简单的示例,它连接到 SQLite 数据库,并在 Web 页面上显示其中的数据: ```python from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db' db = SQLAlchemy(app) class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) def __repr__(self): return f'<Person {self.id}>' @app.route('/') def index(): people = Person.query.all() return render_template('index.html', people=people) ``` 在这个示例中,我们定义了一个名为 Person 的模型,它映射到数据库中的一个表。然后,我们使用 Flask-SQLAlchemy 扩展来连接到 SQLite 数据库,并将其设置为应用程序的默认数据库。 在 index() 视图函数中,我们查询所有的 Person 记录,并将它们传递给一个名为 index.html 的模板,该模板将显示这些记录。 下面是 index.html 模板的代码: ```html <!DOCTYPE html> <html> <head> <title>People</title> </head> <body> <table> <thead> <tr> <th>ID</th> <th>Name</th> </tr> </thead> <tbody> {% for person in people %} <tr> <td>{{ person.id }}</td> <td>{{ person.name }}</td> </tr> {% endfor %} </tbody> </table> </body> </html> ``` 在这个模板中,我们使用一个名为 people 的变量来迭代所有的 Person 记录,并将它们显示在一个 HTML 表格中。 最后,你需要运行这个应用程序,并导航到 http://localhost:5000/,就可以在 Web 页面上看到数据库中的数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值