V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ri0day
V2EX  ›  Python

python sqlite 与 jquery datatables 结合的问题

  •  
  •   ri0day ·
    ri0day · 2015-01-23 18:04:53 +08:00 · 4515 次点击
    这是一个创建于 3353 天前的主题,其中的信息可能已经有所发展或是发生改变。
    今天想把数据库里面的table用 jquery的 datatables 展现一下.前端我完全不懂,照着文档做,发现表格出不来,但是我ajax输出的格式和官方例子里面是一样的。为什么我本地会出不来表格呢

    flask代码:
    from flask import Flask
    import json
    import sqlite3
    db = sqlite3.connect("/Users/min/Downloads/DataTables-1.10.4-trial/hostdb.db",check_same_thread = False)
    cu = db.cursor()
    cu.execute("select * from hostinfo")
    result = cu.fetchall()
    app = Flask(__name__)

    @app.route('/dt')
    def dts():
    d={}
    d['recordsTotal'] = 26
    d["recordsFiltered"] = 26
    d['data'] = result
    print d
    return json.dumps(d)

    app.run(debug=True,port =80)

    html 的代码
    $(document).ready(function() {
    $('#example').dataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": "http://127.0.0.1/dt"
    } );
    } );

    官方例子的输出:(我用chrome看的response)
    {"draw":1,"recordsTotal":57,"recordsFiltered":57,"data":[["Airi","Satou","Accountant","Tokyo","28th Nov 08","$162,700"],["Angelica","Ramos","Chief Executive Officer (CEO)","London","9th Oct 09","$1,200,000"],["Ashton","Cox","Junior Technical Author","San Francisco","12th Jan 09","$86,000"],["Bradley","Greer","Software Engineer","London","13th Oct 12","$132,000"],["Brenden","Wagner","Software Engineer","San Francisco","7th Jun 11","$206,850"],["Brielle","Williamson","Integration Specialist","New York","2nd Dec 12","$372,000"],["Bruno","Nash","Software Engineer","London","3rd May 11","$163,500"],["Caesar","Vance","Pre-Sales Support","New York","12th Dec 11","$106,450"],["Cara","Stevens","Sales Assistant","New York","6th Dec 11","$145,600"],["Cedric","Kelly","Senior Javascript Developer","Edinburgh","29th Mar 12","$433,060"]]}

    我的/dt的输出:
    {"draw":1,"recordsTotal": 26, "recordsFiltered": 26, "data": [["web01.ejuops.com", "10.0.16.1", "datacenter", "webserver", "4c 32g 300g", "ghs55g"], ["web02.ejuops.com", "10.0.16.2", "datacenter", "webserver", "4c 32g 300g", "ghs65g"], ["web03.ejuops.com", "10.0.16.3", "datacenter", "webserver", "4c 32g 300g", "ghsu5g"], ["web04.ejuops.com", "10.0.16.4", "datacenter", "webserver", "4c 32g 300g", "ghsj5g"], ["web05.ejuops.com", "10.0.16.5", "datacenter", "webserver", "4c 32g 300g", "ghs45u"], ["web06.ejuops.com", "10.0.16.6", "datacenter", "webserver", "4c 32g 300g", "ghs45j"], ["web07.ejuops.com", "10.0.16.7", "datacenter", "webserver", "4c 32g 300g", "ghs4ng"], ["web08.ejuops.com", "10.0.16.8", "datacenter", "webserver", "4c 32g 300g", "ghs44g"], ["web09.ejuops.com", "10.0.16.9", "datacenter", "webserver", "4c 32g 300g", "ggs45g"], ["web010.ejuops.com", "10.0.16.10", "datacenter", "webserver", "4c 32g 300g", "bhs45g"], ["web011.ejuops.com", "10.0.16.11", "datacenter", "webserver", "4c 32g 300g", "nhs45g"], ["web012.ejuops.com", "10.0.16.12", "datacenter", "webserver", "4c 32g 300g", "jhs45g"], ["web013.ejuops.com", "10.0.16.13", "datacenter", "webserver", "4c 32g 300g", "khs45g"], ["web014.ejuops.com", "10.0.16.14", "datacenter", "webserver", "4c 32g 300g", "lhs45g"], ["web015.ejuops.com", "10.0.16.15", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web016.ejuops.com", "10.0.16.16", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web017.ejuops.com", "10.0.16.17", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web018.ejuops.com", "10.0.16.18", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web019.ejuops.com", "10.0.16.19", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web20.ejuops.com", "10.0.16.20", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web21.ejuops.com", "10.0.16.21", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web22.ejuops.com", "10.0.16.22", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web23.ejuops.com", "10.0.16.23", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web24.ejuops.com", "10.0.16.24", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web25.ejuops.com", "10.0.16.25", "datacenter", "webserver", "4c 32g 300g", "ghs45g"], ["web26.ejuops.com", "10.0.16.26", "datacenter", "webserver", "4c 32g 300g", "ghs45g"]]}

    现在问题是,既然ajax的输出格式和官网一样的话,我用的是官方的html模版,就改了那个ajax地址,应该出来我要的数据,但是现在就是没出来效果

    我用chrome debug我自己出不来表格的这个页面,在network里面发现ajax请求里的response没有数据,但是我把ajax请求的url贴到浏览器明明有数据.

    哪位有做过这个东西的帮指点一下..
    9 条回复    2015-01-24 01:13:16 +08:00
    lyhapple
        1
    lyhapple  
       2015-01-23 18:53:52 +08:00
    可以向我付费咨询,哈哈.工作中就用jquery datatables
    ri0day
        2
    ri0day  
    OP
       2015-01-23 19:05:17 +08:00   ❤️ 1
    @lyhapple 付费的话 我还好多问题呢。一次性打包吧,我还想用datatables 实现增删改查搜索功能,你能让我理解这些功能的原理和实现方法的话 你开个价格呗。对了,我不会js的.哈哈。
    lyhapple
        3
    lyhapple  
       2015-01-23 19:38:46 +08:00
    @ri0day [email protected] 给我邮件吧..呵呵..
    loading
        4
    loading  
       2015-01-23 20:51:05 +08:00 via Android
    请先尝试 jquery.ajax 看你能不能顺利解析出第一行数据?
    loading
        5
    loading  
       2015-01-23 20:55:37 +08:00 via Android
    试下在页面js直接变量赋值数据,看datatables有没出数据,校验你对dtatables语法和数据源格式的理解!

    自己写js解析下返回的数据,chrome有个叫postman的插件,你看看。


    btw:因为不知道你水平,也没收钱(笑),就说这么多。
    ri0day
        6
    ri0day  
    OP
       2015-01-23 21:10:17 +08:00
    @loading 问题解决了,应该是夸域的问题,我返回不了数据的时候,发现状态是200,但是没有数据,因为我是用浏览器直接本地打开这个html 所以获取不到数据
    后来我尝试 把这个html放到webserver里面用url去访问这个文件,表格就是展示出来了.
    还是谢谢
    loading
        7
    loading  
       2015-01-23 21:15:39 +08:00 via Android
    @ri0day 万万没想到……你居然这样调试……
    loading
        8
    loading  
       2015-01-23 21:16:59 +08:00 via Android
    @ri0day 一般我会直接alert出ajax的数据,没数据出那一下就定位了。
    shajiquan
        9
    shajiquan  
       2015-01-24 01:13:16 +08:00
    恭喜。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3694 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:25 · PVG 18:25 · LAX 03:25 · JFK 06:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.