Ajax asynchronous submission for Python2.7+django1.2+sae blog comments

Source: Internet
Author: User
Tags comments datetime

In the development of the blog system or the electrical business system, in short, usually with replies, comments, replies, such as response to a content of the function, now almost all are using AJAX asynchronous submission, and synchronization in the correct position to display the results of the submission, Instead of refreshing the entire page after the submission to display the content after the submission.

Ajax has been widely promoted as a result of Google's use. It is written in the JavaScript language on the client, uses the XMLHttpRequest object, implements the data interaction with the service, details see: Ajax-xmlhttprequest object.

Ajax submits or obtains the data, the commonly used data form includes:

Ordinary text, the definition of their own rules, their own resolution, no mature class library, need to write their own.

XML literals, parsed using XML parsers, have mature class libraries.

JSON text, a popular form of data, is known for its small size and flexibility, and has a mature class library.

Ajax has several benefits:

Prevent the page from refreshing, because only a part of the content, the page most of the content has not changed, if the refresh and then regain all the words for the server and the client is a burden. And page refreshes can create other problems, such as duplicate submissions.

Reduce server burdens and provide services to more people.

There are some drawbacks:

Programming and debugging to bring some small trouble, not enough to use the habit, there are some methods and tricks.

Because it is Ajax, so the page address does not change, so if you want to share the address, it is difficult to achieve. Additional work needs to be done, such as printing the address and allowing the user to copy and share it.

It is not easy for users to know that their submission is in progress because the page is not refreshed and the user may click the Submit button again. This can also be resolved by submitting the process with a clear hint, and then gray out the Submit button, or do some work to prevent duplicate submissions.

Today's example is the asynchronous submission of blog comments, and the environment is python2.7+django1.2+sae.

The following is the process code in the background, the code in view

@csrf_exempt def comment_new (request,blog_id): blog=get_object_or_404 (blog,pk=blog_id) #str (request.raw_post_data) if request.method== "Post" and Request.is_ajax (): Title=request. post[' Comment_title '] name=request. post[' Comment_author_name '] email=request. post[' Comment_author_email '] url=request. post[' Comment_author_url '] content=request. post[' comment_content '] comment=comment (title=title,author_name=name,author_email=email,author_url=url,content= 
        content); Comment.blog=blog comment=comment.save () _dict={} _dict["title"]=title _dict["au 
          Thor_name "]=name _dict[" Author_email]=email _dict["author_url"]=url "content" _dict[ _dict["Created_at"]=unicode (Datetime.datetime.now ()) return HttpResponse (Simplejson. Jsonencoder (). Encode (str (_dict)) def obj2dict (obj): "" 
    Summary: Convert object to Dict type "" Memberlist = [m for M in Dir (obj)] _dict = {} for M in Memberlist:if M[0]!= "_" and not callable (m): _dict[m] = getattr (obj,m) return _dict

Related Article

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.