[Tornado site building from 0] group chat

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.