简体   繁体   中英

Why is self.render() not working? The front can not receive my message

Everything is working fine except that sending a message to the front in self.render of post. Besides the code after self.render() was running as well. Was this connection shut down? There was no exception happening at all in the backend.

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)

在此处输入图片说明

As you can see, GET and self.render in GET same as the one in POST work. Only this failed, strange. And I send a 404 to the front. The browser recieves without any operation.

在此处输入图片说明 Is there anything wrong with my html?

在此处输入图片说明

Thanks for the help from Sven Festersen. Ajax isn't recieving my data from backend of render. So problom solved in an easy way.

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()

And html is the key for this.

<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>

I changed the script rather than using a render function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM