如何在slite3表头中通过下拉菜单添加过滤器

2024-04-26 18:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python开发一个web应用程序,使用flask和sqlite3数据库。这是一个网络应用程序匿名欺凌报告在学校。 在一个名为tablatestigos的路由中,我想显示一个包含数据库信息的表。表格的标题是“Escuela”、“Curso”、“Victima”和“descripion del hecho”,但我想让“Curso”标题成为一个下拉菜单(就像按标题筛选一样),如果您选择课程5A,它应该只显示该课程的表格行。 我成功地创建了正确的表,并添加了一个下拉菜单,其中的所有课程都在html的标题中,但我不知道如何将它正确地连接到数据库,所以当我单击某个“光标”时,它只显示它的行。 我不希望网页刷新每次一门课程被选中,我希望它发生在日常生活中

这是我的python和flask代码:

@app.route("/tablatestigos", methods=["GET", "POST"])
@login_required
def tablatestigos():
    escuela = session["user_id"]
    hechos = db.execute("SELECT * FROM testigos WHERE escuela = :escuela", escuela=session["user_id"])
    if not hechos:
        return apology("No se han recibido reportes aún.")
    return render_template("tablatestigos.html", hechos=hechos)

这是我的html:

{% extends "layout.html" %}

{% block title %}
    Reportes de Testigos
{% endblock %}

{% block main %}
    <form action="/" method="post">
        <table class="table table-striped">
        <thead>
            <tr>
                <th>Escuela</th>
                <th>Curso</th>
                <th>Victima</th>
                <th>Descripción del hecho</th>
                <th>
                  <div class="dropdown">
                    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Todos
                      <span class="caret"></span></button>
                      <ul class="dropdown-menu" name="curso">
                        {% for hecho in hechos %}
                        <li><a href="#">{{ hecho.curso }}</a></li>
                        {% endfor %}
                      </ul>
                  </div>
                </th>

            </tr>
        </thead>

        <tbody>

    {% for hecho in hechos %}
                <tr>
                    <td>{{ hecho.escuela }}</td>
                    <td>{{ hecho.curso }}</td>
                    <td>{{ hecho.victima }}</td>
                    <td>{{ hecho.hecho }}</td>

                </tr>
            {% endfor %}

        </tbody>
    </table>
    </form>

{% endblock %}

抱歉,如果这是一个非常noob的问题,但我是新的编码,这是我的第一个网络应用程序项目,我正在开发。 提前感谢:)


Tags: 数据库标题htmltabletrclass课程td