【问题标题】:Sending html table data to a flask method and retrieve information from the database将 html 表数据发送到烧瓶方法并从数据库中检索信息
【发布时间】:2020-07-10 03:08:22
【问题描述】:

我一直在使用用于 DB 的 sqlite3 在 Flask 中开展一个小型项目。该项目是关于如何保护患者的医疗记录。我有两个视图,一个是让管理员插入新患者并查看他们的详细信息,另一个是让患者查看他们的记录。 我的问题是,当管理员插入新患者时,该特定患者的姓名和 ID 将被添加到动态 html 表以及该表中的患者列表中,他可以单击特定患者查看他/她的记录。但我无法映射患者记录的查看部分。 我收到一个运行时错误,上面写着 TypeError: The view function did not return a valid response。该函数要么返回 None 要么在没有返回语句的情况下结束 以下是模板:

包含患者姓名和 ID 的动态表:

 {% block content %}
    <form method="post" action="{{ url_for('custom') }}">
<table>
  <thead>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
  </thead>

    {% for row in rows %}
    <tr>


                <td name="userid1"><a href="{{ url_for('custom') }}" >{{ row.Userid }}</a></td>
                <input type="hidden" id="id" name="userid1"  value=" {{ row.Userid }}"/>
                <td name="names1"><a href="{{ url_for('custom') }}" > {{row.Name}}</a></td>
                <input type="hidden" id='na' name="names1" value="{{row.Name}}" />
                <td> {{ row["Age"]}}</td>
            </tr>
         {% endfor %}
</table>
    <br><br>
    </form>``
{% endblock %}

病历页面(查看):

</div>
</div>
<div class="details" style="height:550px;" >
    <img src="static/images/bg2.png" style="width:250px;height:300px;margin-top:3%;">
    <div class="contentin">
      <p class="tit1"><b>{{ infos[1] }}</b></p>
      <b><p class="tit2">Age:&nbsp;&nbsp;<font style="color:black;">{{ infos[2] }}</font></p>
      <p class="tit2">Sex:&nbsp;&nbsp;<font style="color:black;">{{ infos[3] }}</font></p>
      <p class="tit2">Height:&nbsp;&nbsp;<font style="color:black;">{{ infos[4] }}</font></p>
      <p class="tit2">Weight:&nbsp;&nbsp;<font style="color:black;">{{ infos[5] }}</font></p>
       <p class="tit2">Sugar Level:&nbsp;&nbsp;<font style="color:black;">{{ infos[6] }}</font></p>
       <p class="tit2">BP Level:&nbsp;&nbsp;<font style="color:black;">{{ infos[7] }}</font></p>
       </b>
      <button class="btn" style="width:350px;"><font style="color:white;"><b>Download All Reports&nbsp;&nbsp;<i class="fa fa-download"></i></button>&nbsp;&nbsp;
<button class="btn" style="width:300px;">Share&nbsp;&nbsp;<i class="fa fa-share-alt"></i></b></font></button></center>

    </div>
</div>

我的 Flask .py 文件:

@app.route('/patientinfo')
def patientinfo():
    conn = sqlite3.connect('patients.db')
    conn.row_factory = sqlite3.Row

    c = conn.cursor()
    c.execute("select Userid,Name,Age from details")

    rows = c.fetchall()
    return render_template("patientinfo.html", rows=rows)


@app.route('/hospital', methods=['GET', 'POST'])
def custom():
    User_ID = request.form.get("userid1")
    Name = request.form.get("names1")
    conn = sqlite3.connect('patients.db')
    cul = conn.cursor()
    cul.execute('SELECT Userid, Name, Age, Sex, Height, Weight, Sugar, BP from details')
    pt = cul.fetchall()
    for pat in pt:
        if pat[0] == User_ID:
            if pat[1] == Name:
                return render_template("hospital.html", infos=pat)

【问题讨论】:

    标签: python html forms sqlite flask


    【解决方案1】:

    当用户点击&lt;td&gt; 中的链接时,会发送一个GET 请求。这个request.form.get("userid1") 将返回Nonerequest.form.get("names1") 也将返回)。这个if pat[0] == User_ID: 永远不会是真的。程序失败并且不返回有效响应。这就是它发生的原因。由于这是患者数据(从美国的角度来看),您可能不应该在查询字符串(即 GET 请求)中发送 id/name。也许必须引入javascript。花一些时间考虑选项,即。这个新信息。

    【讨论】:

      猜你喜欢
      • 2020-02-29
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 2023-03-11
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多