Main Code of the group chat front-end:
{% Block content %} <! -- <P class = 'text-success h3 '> test version. enjoy it ~~ :-) </P> --> <ul> {% for I in content [19 * int (num)-19: 19 * int (num)] %} <li class = 'text-info'> <a href = '/user/{I [1]}' class = 'text-success h4 '> 【{ {I [1] }}] </a> ({I [3]}): {I [2] }}</li >{% end %} </ul> <ul class = 'pagination'> {% for I in range (1, pages + 1) %} <li {% if int (num) = I %} class = 'active' {% end %}> <a href = '/chat/{I}' >{{ I} </a> </li >{% end % }</ul> </br> <form action = '/chat/1' method = 'post' class = 'Well form-search'> <label class = 'sr-only'> post new stickers </label> <input type = 'text' name = 'topic 'placeholder =' post new stickers ... '/> <button type = 'submit' class = 'btn btn-danger '> send </button> </form >{% end %}
Background code:
Class chatHandler (tornado. web. requestHandler): def get (self, num): # num Page name = self. get_cookie ('hackername') chats = showChat () n = len (chats) if n % 20 = 0: pages = n // 20 # Total pages of pages else: pages = n // 2018.1self.render('chat.html ', cookieName = name, content = chats, pages = pages, num = num) def post (self, num): name = self. get_cookie ('hackername') if not name: return # In javascript, determine if newTopic = self has been logged on. get_argument ('topic ') insertChat (name, newTopic) self. redirect ('/chat/1 ')
The showChat function is defined as follows:
Def showChat (): c.exe cute ('select * from chat') tmp = c. fetchall () return tmp [:-1] # reverse order, with the latest post placed at the top
The insertChat function is defined as follows:
def insertChat(name,content):now=getTime()c.execute('insert into chat(name,content,time) values("'+name+'","'+content+'","'+now+'")')db.commit()
As follows:
The group chat content is sorted in reverse chronological order. The newest is at the top, and a page is added when 20 items are full. The default group chat homepage is/chat/1, which is the first page. Here, we only implement basic functions, and then add real-time ajax refresh. New message prompts are all front-end things. After all the basic functions are implemented, we add them.
Reprinted Please note: