[Start from 0 to build tornado sites] group chat

Source: Internet
Author: User
Tags button type

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

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.