﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-不可思议之光景-随笔分类-Python</title><link>http://www.blogjava.net/stingh711/category/26267.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 17 Nov 2007 22:09:47 GMT</lastBuildDate><pubDate>Sat, 17 Nov 2007 22:09:47 GMT</pubDate><ttl>60</ttl><item><title>My first ajax app using django and jquery</title><link>http://www.blogjava.net/stingh711/archive/2007/11/16/161014.html</link><dc:creator>django</dc:creator><author>django</author><pubDate>Fri, 16 Nov 2007 06:48:00 GMT</pubDate><guid>http://www.blogjava.net/stingh711/archive/2007/11/16/161014.html</guid><wfw:comment>http://www.blogjava.net/stingh711/comments/161014.html</wfw:comment><comments>http://www.blogjava.net/stingh711/archive/2007/11/16/161014.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stingh711/comments/commentRss/161014.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stingh711/services/trackbacks/161014.html</trackback:ping><description><![CDATA[
		<b>Requirement:</b>
		<br />There is a simple form that has two input field, one is zip code and another is city. If the zip code is filled, the page will fill the city automatically by getting the relevant city of the zip code in an AJAX way.

<br /><br /><b>Implementation:</b><br />1. django-admin.py startproject study<br />2. cd study<br />3. python manage.py startapp ajax<br />4. Edit settings.py, add 'study.ajax' to INSTALLED_APPS.<br />5. mkdir site_media<br />6. Download jquery.js from www.jquery.com and copy it to site_media<br />7. Add the following line to urls.py: <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">(r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^site_media/(?P&lt;path&gt;.*)$</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">django.views.static.serve</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, {</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">document_root</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">/home/sting/Django/study/site_media</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">}),</span></div>  Note: We will include jquery.js in our html, for this kind of static files, django's handling is different from others. Please see <a target="_blank" href="http://www.djangoproject.com/documentation/static_files/">http://www.djangoproject.com/documentation/static_files/</a> to get more detailed information.<br />8. cd ajax<br />9. mkdir templates<br />  Note: This folder is used to put template files. It seems that django can load template files from this folder automatically, so you needn't to configure it in settings.py.<br />10. Create the following html file named form.html.<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">&lt;!</span><span style="color: rgb(255, 0, 255);">DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">html</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">head</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">Register form</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">title</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">script </span><span style="color: rgb(255, 0, 0);">src</span><span style="color: rgb(0, 0, 255);">="http://localhost:8000/site_media/jquery.js"</span><span style="color: rgb(255, 0, 0);"> type</span><span style="color: rgb(0, 0, 255);">="text/javascript"</span><span style="color: rgb(0, 0, 255);">&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">script</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">script </span><span style="color: rgb(255, 0, 0);">type</span><span style="color: rgb(0, 0, 255);">="text/javascript"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);"><br />  </span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 255);">function</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);"> getZipData() {<br />    </span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 255);">var</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);"> zipcode </span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);">=</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);"> $('#zip').val();<br />    $.get('http:</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 128, 0);">//</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 128, 0);">localhost:8000/ajax/getzipdata/' + zipcode + '/', function(data) {</span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 128, 0);"><br /></span><span style="background-color: rgb(245, 245, 245); color: rgb(0, 0, 0);">      $('#city').attr({'value':data});<br />    });<br />  }<br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">script</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">head</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">body</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">table</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">form </span><span style="color: rgb(255, 0, 0);">actioon</span><span style="color: rgb(0, 0, 255);">=""</span><span style="color: rgb(255, 0, 0);"> method</span><span style="color: rgb(0, 0, 255);">="post"</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tr</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">Zip:</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">input </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="zip"</span><span style="color: rgb(255, 0, 0);"> onblur</span><span style="color: rgb(0, 0, 255);">="getZipData()"</span><span style="color: rgb(255, 0, 0);"> type</span><span style="color: rgb(0, 0, 255);">="text"</span><span style="color: rgb(255, 0, 0);"> name</span><span style="color: rgb(0, 0, 255);">="zip"</span><span style="color: rgb(0, 0, 255);">/&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">tr</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">tr</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);">City:</span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">td</span><span style="color: rgb(0, 0, 255);">&gt;&lt;</span><span style="color: rgb(128, 0, 0);">input </span><span style="color: rgb(255, 0, 0);">id</span><span style="color: rgb(0, 0, 255);">="city"</span><span style="color: rgb(255, 0, 0);"> type</span><span style="color: rgb(0, 0, 255);">="text"</span><span style="color: rgb(255, 0, 0);"> name</span><span style="color: rgb(0, 0, 255);">="city"</span><span style="color: rgb(0, 0, 255);">/&gt;&lt;/</span><span style="color: rgb(128, 0, 0);">tr</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">form</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">table</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">body</span><span style="color: rgb(0, 0, 255);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">html</span><span style="color: rgb(0, 0, 255);">&gt;</span></div>11. Edit views.py and add the following methods.<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.http </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> HttpResponse<br /></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.core </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> serializers<br /></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> django.shortcuts </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> render_to_response<br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> getzipdata(request, zipcode):<br />  </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> zipcode </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">214028</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:<br />    city </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">wuxi</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />    city </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"><br /><br />  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> HttpResponse(city)<br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> register(request):<br />  </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> render_to_response(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">form.html</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, {})</span></div>12. Add the following lines to urls.py.<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">(r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^ajax/register/$</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">study.ajax.views.register</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),<br />(r</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">^ajax/getzipdata/(\d+)/$</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">study.ajax.views.getzipdata</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">),</span></div><br />Visit http://localhost:8000/ajax/register/, you will see the simple form, after fill the zip code and move to city, the city will be filled automatically.<br /><p style="text-align: right; font-size: 8px;">Blogged with <a href="http://www.flock.com/blogged-with-flock" title="Flock" target="_new">Flock</a></p><!-- technorati tags begin --><p style="font-size: 10px; text-align: right;">Tags: <a href="http://technorati.com/tag/django" rel="tag">django</a>, <a href="http://technorati.com/tag/jquery" rel="tag">jquery</a>, <a href="http://technorati.com/tag/%20ajax" rel="tag"> ajax</a>, <a href="http://technorati.com/tag/%20python" rel="tag"> python</a></p><!-- technorati tags end --><img src ="http://www.blogjava.net/stingh711/aggbug/161014.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stingh711/" target="_blank">django</a> 2007-11-16 14:48 <a href="http://www.blogjava.net/stingh711/archive/2007/11/16/161014.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>IPython, an enhanced python &amp;quot;shell"</title><link>http://www.blogjava.net/stingh711/archive/2007/11/06/158589.html</link><dc:creator>django</dc:creator><author>django</author><pubDate>Tue, 06 Nov 2007 08:32:00 GMT</pubDate><guid>http://www.blogjava.net/stingh711/archive/2007/11/06/158589.html</guid><wfw:comment>http://www.blogjava.net/stingh711/comments/158589.html</wfw:comment><comments>http://www.blogjava.net/stingh711/archive/2007/11/06/158589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/stingh711/comments/commentRss/158589.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/stingh711/services/trackbacks/158589.html</trackback:ping><description><![CDATA[As the definition on the Internet, IPython is an enhanced interactive Python shell. It has a lot of attractive features over the original Python shell. I take a quick look at them, and list my favorites.
<ul><li>Auto completion with "Tab". For example, s is a string, if you press "Tab" after "s.", all methods of Python string will be listed, just like the auto completion in my konsole.</li><li>Auto indent. 
</li><li>IPython will add parentheses to functions and methods. For example, dir '' is the same with dir('')</li><li>Magic commands. Common used shell commands are also available in IPython shell, such as "cd", "ls", "mkdir". Another two magic commands are very useful. The first one is "edit". In IPython shell, command "edit xxx" will invoke vim to edit the xxx file. (Vim is the default editor and emacs is supported too.) Another useful command is run. You can use command "run xxx.py" to run xxx. There are lots of other magic commands that I haven't tested. You can use command "lsmagic" to list them.</li><li>Dynamic object information. Type "?word" and "??word" prints detailed information about an object. 
</li><li>System shell access. Any shell commands can be used in IPython by add "!" in the front. For example, "!ifconfig" will pass "ifconfig" to the shell directly.
</li></ul>There are many other features like "session logging and restoreing", "Input and output caching" that I haven't tested yet.
If you are learning Python, I strongly recommend it to you. <br />Let's <a href="http://ipython.scipy.org/moin/">try</a> it together!
   <p style="text-align: right; font-size: 8px;">Blogged with <a href="http://www.flock.com/blogged-with-flock" title="Flock" target="_new">Flock</a></p><!-- technorati tags begin --><p style="font-size: 10px; text-align: right;">Tags: <a href="http://technorati.com/tag/python" rel="tag">python</a></p><!-- technorati tags end --><img src ="http://www.blogjava.net/stingh711/aggbug/158589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/stingh711/" target="_blank">django</a> 2007-11-06 16:32 <a href="http://www.blogjava.net/stingh711/archive/2007/11/06/158589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>