The python example-django the JSON to the JS script.

Source: Internet
Author: User

One, there are two ways to pass Sjon to the page, one is Ajax use, and the other is directly rendered in the page:

views.py

#Coding:utf-8 fromDjango.shortcutsImportRenderImportJSON#Create your views here.defAddjson (Request): List_= ['List Collection','render json to template'] Dict_= {'site':'Dictionary Address','author':'self-study person'}        returnRender (Request,'addjson.html',{'jsonlist': Json.dumps (List_),'Dict': Json.dumps (dict_)})#Remember to use Json.dumps to handle this, otherwise you will get an error.

urls.py

 fromDjango.conf.urlsImportinclude, url fromDjango.contribImportAdminurlpatterns= [URL (r'^.*?json/$','Learn.views.addjson', name='Addjson'), url (r'^admin/', include (Admin.site.urls)),]

Addjson.html

<!DOCTYPE HTML><HTML><Head><title>Welcome to JSON</title><Scriptsrc= "Http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></Script><!--script src= "Http://code.hs-cn.com/jquery/jquery-1.7.1.min.js" ></script -</Head><Body><DivID= "List">Learn out JSON:</Div><DivID= ' Dict '></Div><Scripttype= "Text/javascript">        //List        varList={{Jsonlist|Safe}};        //Remember to use safe to filter oh, otherwise you will get an error. //The following code puts each part of the list to the head and tail        $('#list'). prepend (list[0]); $('#list'). Append (list[1]); Console.log ('---traverse List Method 1---');  for (varI=List.length- 1; I>= 0; I--){                //right mouse button, audit element, select Console to see the value enteredConsole.log (List[i]);        }; Console.log ('---Traverse both the index and the content, using the Jquery.each () method---'); $.each (List,function(Index,item) {console.log (index);        Console.log (item);        }); //Dictionary        varDict={{Dict|Safe}}; Console.log ('---The value of two dictionaries---'); Console.log (dict['site']);        Console.log (Dict.author); Console.log ("---traverse the dictionary---");  for(iinchDict) {Console.log (i+Dict[i]); //Note that I is the key value here        };</Script></Body>

Start the service, the front desk access http://localhost:8000/json/can see the effect.

October 22, 2015 22:59:56

The python example-django the JSON to the JS script.

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.