为什么self.render()不起作用?前台无法收到我的消息

时间:2018-09-04 08:09:08

标签: python html5 tornado

一切正常,除了在self.render post中发送消息到前台。 除了self.render()之后的代码也正在运行。此连接是否关闭?后端完全没有例外。

class ListsHandler(tornado.web.RequestHandler):
        def get(self):
            print('get')
            agent_id = self.get_argument('agent_id', None)
            answer_sql = 'select id,value from answer_list'
            cursor.execute(answer_sql)
            answer_results = cursor.fetchall()
            question_sql = 'select id,value from question_list'
            cursor.execute(question_sql)
            question_results = cursor.fetchall()
            answer_results=list(answer_results)
            question_results = list(question_results)
            print 'answer:',answer_results
            print 'question',question_results

            self.render("lists.html", agent_id=agent_id, answer_results=answer_results, question_results=question_results)

        def post(self):

            id = self.get_argument('id', None)
            id=int(id)
            agent_id = self.get_argument('agent_id')  
            if id:
                id-=1
                new_id=id-1
                print(agent_id, id)
                cursor.execute('delete from question_list where id=%s' % (id))
                cursor.execute('update question_list set id=id-1 where id>%s' % (id))
                cursor.execute('delete from answer_list where id=%s' % (id))
                cursor.execute('update answer_list set id=id-1 where id>%s' % (id))
                db.commit()
                answer_sql = 'select id,value from answer_list'
                cursor.execute(answer_sql)
                answer_results = cursor.fetchall()
                question_sql = 'select id,value from question_list'
                cursor.execute(question_sql)
                question_results = cursor.fetchall()
                question_dict[agent_id] = question_results
                self.render("lists.html", agent_id=agent_id, answer_results=answer_results,
                            question_results=question_results)

enter image description here

如您所见,GET中的GET和self.render与POST工作中的相同。只有这样失败了,奇怪。 然后我将404发送到前面。浏览器接收到没有任何操作。

enter image description here 我的html有什么问题吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

感谢Sven Festersen的帮助。 Ajax没有从render后端接收我的数据。因此问题可以轻松解决。

def post(self):
    # id为此条问答的id,是lists中显示的id-1
    id = self.get_argument('id', None)
    agent_id = self.get_argument('agent_id')  # 获取用户场景id
    pn = self.get_argument('pn', 1)
    pn = int(pn)
    if id:
        id = int(id)
        id-=1
        new_id=id-1
        cursor.execute('delete from question_list where id=%s' % (id))
        cursor.execute('update question_list set id=id-1 where id>%s' % (id))
        cursor.execute('delete from answer_list where id=%s' % (id))
        cursor.execute('update answer_list set id=id-1 where id>%s' % (id))
        db.commit()

而html是实现这一目标的关键。

<script>
            $(function(){
                $('#navHeight').height($(document).height()-85);
                $('#table').on('click','tr .del',function(){
                    var id=$(this).attr('data-id');
                    $.ajax({
                        url:'http://10.240.100.38:8888/lists.html?agent_id='+{{agent_id}},
                        type:"POST",
                        data:{_method:"POST",id:id},
                        success : function(data) {
                            location.reload(true);
                        }
                    })
                })
            })
        </script>

我更改了脚本,而不是使用渲染功能。