将数据库(mysql)中的数据打印到flas中的表中

2024-09-28 20:48:59 发布

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

当有两个搜索框时,点击“服务器”按钮从数据库中带出与所选两个选择框选项匹配的数据。另外,服务器将在表中显示数据。 所以我在google上搜索do-this,发现人们使用append将数据插入表中。但它不起作用,我终于得到了jsonify不返回列表。。。我怎样才能解决这个问题?在

应用程序副本

@app.route('/solution')
def solution():
    # return render_template('solution.html')
    return gijunController.getGijun()


# It is just bring data from mysql to select box in html
@app.route('/_update_gijun')
def update_gijun():
    # print(data)
    return gijunController.updateTrouble()

# hava a problem here..
@app.route('/_show_gijun_table')
def show_gijun_table():
    return gijunController.showGijunTable()

gijunController.py

^{2}$

counselingdao.getStandardBigo从数据库中带来titlecategory1category2content。同样,结果如下:

[{'category_name': 'Korea', 'type_1': 'seoul', 
'standard': 'capital', 'bigo': ''}, 
{'category_name': 'Korea', 'type_1': 'seoul', 
'standard': 'history', 'bigo': ''}, 
{'category_name': 'Korea', 'type_1': 'sightseeing', 
'standard': '', 'bigo': ''}]

(standard和bigo可以为空)

解决方案.html

<div class="row">
<div class="col-sm-12">
<form>
  <div class="form-row">
    <div class="form-group col-sm-3">
      <label for="Upjong">Category</label>
      <select class="form-control" id="Upjong" name="Upjong">
        {% for row in result %}
          <option value="{{ row.category_name }}">{{ row.category_name }}</option>
        {% endfor %}
      </select>
    </div>
    <div class="form-group col-sm-3">
      <label for="Trouble1">Trouble Type1</label>
      <select class="form-control" id="Trouble1" name="Trouble1">
        {% for row in Trouble1 %}
          <option value="{{ row }}">{{ row }}</option>
        {% endfor %}
      </select>
    </div>
    <div style="margin-top:32px;">
      <button type="submit" class="btn btn-outline-dark" id="show_gijun_table">search</button>
    </div>
  </div>
</form>
</div>
</div>

<div class="row">
  <div class="col" id="show_gijuns">
    {% include 'solution_table.html' %}
  </div>
</div>

解决方案_表格.html

{% if datas %}
<table id="gijuns" style="width:100%">
    <thead>
    <tr>
        <th>Category</th>
        <th>Trouble Type1</th>
        <th>Gijun</th>
        <th>Bigo</th>
    </tr>
    </thead>
    <tbody>
    {% for row in datas %}
        <tr>
            <td> {{ row.category_name }}</td>
            <td> {{ row.type_1 }}</td>
            <td> {{ row.standard }}</td>
            <td> {{ row.bigo }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
{% endif %}

js

$(document).ready(function () {
    $('#Upjong').change(function () {

        $.getJSON('/_update_gijun', {
            selected_class: $('#Upjong').val()

        }).success(function (data) {
            $('#Trouble1').html(data.html_string_selected);
        })
    });

    $('#show_gijun_table').bind('click', function () {
        // solution 1
        $.getJSON('/_show_gijun_table', {
            selected_class: $('#Upjong').val(),
            selected_entry: $('#Trouble1').val(),
        }).success(function (data) {
            datas = JSON.stringify(data['datas'])
            console.log("This is the returned data: " + data);
            var str='<tr>'
            $.each(datas, function(i){
                str += '<td>'+datas[i].category +'</td><td>'+ datas[i].type_1+'</td><td>'+datas[i].standard+'</td><td>'+datas[i].bigo+'</td>';
                str += '</tr>';
            });
            $('#show_gijuns').append(str);
        });

        return false;
    });
});

(+) 这是另一个问题,有人对我说这段代码不是MVC模式。尤其是他说

selected_class: $('#Upjong').val(),
selected_entry: $('#Trouble1').val(),

这部分不好。我记不清他到底说了什么,但我想他说的是“当客户机请求时,服务器必须处理好一切。”。但是,这段代码不是goo,因为客户端发送的是值 所以我试图修复它,但未能从选定的选项中获取值。除了上面的代码,我怎么知道用户选择了哪个选项?在


Tags: namedivformdatahtmlshowtableclass