Main Code of the group chat front-end:
{% Block content %} <! -- <P class = 'text-success H3 '> trial version number. 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 # deduce in Javascript whether 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()
For example:
The group chat content is sorted in reverse chronological order. The latest chat content is at the top. If there are 20 chats, add them? The default group chat homepage is/chat/1, that is, the first page. Here, we only implement the basic functions, and then add real-time Ajax refresh. New message prompts are all front-end things, which are added after all the basic functions are implemented.
Reprint Please note: transferred from http://blog.csdn.net/littlethunder/article/details/25560003
[Start from 0 to build tornado sites] group chat