Django uses a template for dynamic partial refresh

Source: Internet
Author: User

I want to learn python, but I cannot find a direction. Recently I want to suddenly be interested in Web programming. I 've been watching it all the time. I just made some things to the tutorial.

 

When using Django to submit comments, if you use form to submit comments, it is best to display the comments you just entered after the submission is successful;

Therefore, after you submit a comment, it is best to immediately display the comment.

Three methods were taken into account:

1. At first, consider using Ajax + JSON to read the latest comment from the server. When multiple users submit comments at the same time, this method will not be synchronized and pass off;

2. The client still uses Ajax + JSON to pass the number of comments on the current page. The server returns all comments not displayed on the client and loads these comments in the Ajax callback function;

3. The client retrieves comments from the server through Ajax. The server uses the template to render all comments as HTML text content and returns the content to the client, the client inserts the received HTML text into the div.

In methods 1 and 2, JSON data needs to be parsed in JS Code and HTML content is generated based on JSON data. Method 3 can be used for unified processing using templates. Compared with processing JSON data and rendering templates, method 3 will increase the burden on the server.

 

The following code is used:

Comment mudel

1 class blogcontent (models. model): 2 blog = models. foreignkey (blog) 3 auth = models. charfield ('nicknames ', max_length = 40) 4 CONTENTS = models. textfield ('content', max_length = 2000) 5 time = models. timefield (auto_now_add = true) 6 date = models. datefield (auto_now_add = true)

Client form

{# The following is the comment input area #}< Div id = "blogcomment"> <Form ID = "user_content"> <div> nickname <input type = "text" id = "username ""value =" anonymous "> <p/> </div> <textarea id =" content_value "> </textarea> <p/> <input type =" text "id = "blog_id" value = "{item. id} "style =" display: none; "/> <input type =" Submit "id =" content_submit "value =" submit comments "/> </form> </div>

JS Code for submitting comments

1 $ (function () {2 $ ("# user_content "). submit (function () {3 var username = $ ("# username "). val (); 4 var content_value = $ ("# content_value "). val (); 5 var blog_id = $ ("# blog_id "). val () 6 $ (this ). ajaxsubmit ({7 Type: "Post", // submission method 8 datatype: "text", // data type 9 URL: "/content/", // request url10 data: {11 'username': username, 12 'content _ value': content_value, 13 'blog _ id': blog_id14}, 15 success: function (data) {// callback function 16 loadnewcontents () 17 $ ("# content_value "). val (""); 18} 19}); 20 return false; // do not click New Page 21}); 22 });

JavaScript code that reads all comments from the server and displays them

1 // load the latest comments 2 function loadnewcontents () 3 {4 var lstcontent =$ ("# lstcontents"); 5 // lstcontent.html (""); 6 7 var blog_id = $ ("# blog_id "). val () 8 $ (this ). ajaxsubmit ({9 type: "Post", // submission method 10 datatype: "text", // data type 11 URL: "/allcontent/", // request url12 data: {13 'blog _ id': blog_id14}, 15 success: function (data) {// callback function 16 if (data. length> 0) 17 {18 $ ("# lstcontents" ).html (data); 19 20} 21} 22}); 23}

Django Template

1 {% for item in list_contents %} 2 <Div class = "content"> 3 <Div class = "content_info" >#{{ forloop. counter }}floor {item. auth }}posted on {item. date }}{ {item. time }}< P/> </div> 4 <Div class = "content_value" >{{ item. contents }}</div> 5 <Div class = "content_option"> <a href = "#"> reply </a> <a href = "#"> reference </ a> </div> 6 </div> 7 {% endfor %}

Django views

 1 @csrf_exempt 2 def get_blog_all_contents(request): 3     response = HttpResponse() 4     response[‘Content-Type‘] = "text/json" 5     strId = request.POST.get("blog_id", ‘‘) 6     if strId: 7         try: 8             blog_id = int(strId) 9             item = Blog.objects.get(id=blog_id)10             list_contents = item.blogcontent_set.order_by(‘date‘, ‘time‘)11             return render_to_response("blogContentList.html", locals())12         except Exception, e:13             print(e)14     return ""

 

Django uses a template for dynamic partial refresh

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.