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.