使用Flask动态HTML内容和Python变量 [英] Dynamic HTML content and Python variables with Flask

查看:1034
本文介绍了使用Flask动态HTML内容和Python变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户端,说我有人通过表格选择学校记录在下面的表格中显示。例如,他们从名称列表中选择名称,并从数据库中提取记录。

根据他们的班级年份,他们可能会有1,2,3或4年的记录,所以所拉取的数据总是会有所不同。 b
$ b

提交表格后,他们的记录被存储在一个包含所有记录的变量中,然后每个记录被分解成多个子类型,例如英文部门的所有记录。这些子类型存储在其他列表中 - 所以有一个列表,所有的英文记录。我们称之为变量english_records。我想要使​​用这些子类型变量只能显示用户想要查看的数据,并显示该特定列表中的所有数据。所以使用Flask的render_template函数,我试图发送每个这些记录到一个html模板,它将为每个记录创建一个表格单元格。



b
$ b

我一直在尝试的(到目前为止还没有工作)是这样的:

Python:

  i = 0 
def index():
for english_records:
english_records = [
{
'english':english_records [i]
}
]
i = i + 1
return render_template(index。 html,
english_records = english_records)

在HTML中:

  ...上面的表格... 
{记录在english_records%中的%}
< ; TD>
{{record.english}}
< / td>
{%endfor%}
...表继续...

到目前为止,我已经为每个记录获取表格单元格,但记录没有通过。任何人都知道如何做到这一点?

解决方案

是否有一个原因,你要预处理的数据?什么 english_records 看起来像?如果我的预感是正确的,那么你根本就不需要作为循环。



还有在for循环中,用 english_records 覆盖了最初的 english_records 变量,所以这个赋值是合法的语法上的术语,在逻辑上是荒谬的。

另一个问题是,根据第一个 english_records 的实际类型, ,你不需要使用计数器:如果 english_records list ,那么它将包含值您正在寻找。如果 english_records 是一个 dict ,那么枚举它可能看起来像键,val在english_records .iteritems()


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 %}
<td>
  {{record.english}}
</td>
{% 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().

这篇关于使用Flask动态HTML内容和Python变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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