[Tornado website building from scratch] post articles and comments

Source: Internet
Author: User
Tags translate function

First:


This is the home page of every user. Because it is a basic function, there is no profile picture, hobbies, or other information for the user, which will be added in the next stage. Click the "post new article" button on the right to go to the post page:



I have tried some open-source Rich Text editors widgEditor and Baidu's ueditor before. They always feel too popular. Since the domain name is hacker, I should use the hack method, directly use the bare <textarea> and then use the markdown format to make it the best. After posting, it looks like:



Supports markdown perfectly, and all the markdown editors I have tested, such as retext and dillinger. io, atom, and stackedit do not support "<" and ">" in <pre>, so in these editors, # include <stdio. h> is printed out, I use python-markdown2 transcoding will not have this problem, pay attention to # Before using "\" Escape, otherwise think of the Main title in markdown syntax, the font will become larger. The front-end code for posting an article is as follows:

{% Block content %} <form action = '/Post' method = 'post' class = 'well'> <div class = 'form-group'> <label class =' sr-only '> title </label> <input type = 'text' name = 'title' class = 'form-control' placeholder = 'enter title'/> </ div> <div class = 'form-group'> <label class = 'sr-only'> body </label> <textarea rows = '20' type = 'text' name = 'blog 'class = 'form-control' placeholder = 'Enter the document body'> </textarea> </div> <button type = 'submit' class = 'btn btn -warning '> send </button> </form> {% end %}

The back-end code for posting the article is as follows:

class postHandler(tornado.web.RequestHandler):def get(self):name=self.get_cookie('hackerName')self.render('post.html',cookieName=name)def post(self):title=self.get_argument('title')blog_md=self.get_argument('blog')blog=translate(blog_md)name=self.get_cookie('hackerName')idvalue=insertBlog(name,title,blog)self.redirect('/blog/'+str(idvalue))

The insertBlog function is defined as follows:

Def insertBlog (name, title, blog): now=time.ctime()c.exe cute ('insert into blog (name, title, blog, time) values ("'+ name + '", "'+ title +'", "'+ blog +'", "'+ now +'" comment 'mongodb.commit()c.exe cute ('select max (id) from blog ') return c. fetchone () [0] # id of the newly inserted data

The translate function is defined as follows:

def translate(md):for i in whiteList:if i[0] in md:md=md.replace(i[0],i[1])md2=html.escape(md)data=markdown2.markdown(md2)for i in whiteList:if i[1] in data:data=data.replace(i[1],i[0])return data

The main code for displaying the previous section of the article is as follows:

{% Block content %} <div> <p class = 'text-success h2 '>{{ blog [1]} column </p> <br/> <p class = 'text-danger h3 '>{{ blog [2] }}</p> <p class = 'text-muted h6' >{{ blog [4]} </p> <br/> {% raw blog [3] %} </div> <ul> {% for I in comments %} <li> 

The backend code for displaying articles and comments is as follows:

class blogHandler(tornado.web.RequestHandler):def get(self,idvalue):selfname=self.get_cookie('hackerName')blog=showOneBlog(idvalue)comments=showComment(idvalue)self.render('blog.html',cookieName=selfname,blog=blog,comments=comments)class commentHandler(tornado.web.RequestHandler):def post(self):selfname=self.get_cookie('hackerName')comment=self.get_argument('comment')refer=self.request.headers.get('referer')for i in range(len(refer)-1,0,-1):if refer[i]=='/':breakblogid=refer[i+1:]print(blogid)addComment(blogid,selfname,comment)self.redirect('/blog/'+blogid)

The document and comment table are defined as follows:

CREATE TABLE blog(id integer primary key,name text,title text,blog text,time text);CREATE TABLE comment(id integer primary key,blogid integer,name text,comment text,time text);

I want to add the comment @ function in the next upgrade and the comment prompt function.

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.