Requirement:
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.

Implementation:
1. django-admin.py startproject study
2. cd study
3. python manage.py startapp ajax
4. Edit settings.py, add 'study.ajax' to INSTALLED_APPS.
5. mkdir site_media
6. Download jquery.js from www.jquery.com and copy it to site_media
7. Add the following line to urls.py:
(r'^site_media/(?P<path>.*)$''django.views.static.serve', {'document_root':'/home/sting/Django/study/site_media'}),
  Note: We will include jquery.js in our html, for this kind of static files, django's handling is different from others. Please see http://www.djangoproject.com/documentation/static_files/ to get more detailed information.
8. cd ajax
9. mkdir templates
  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.
10. Create the following html file named form.html.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Register form</title>
<script src="http://localhost:8000/site_media/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
  
function getZipData() {
    
var zipcode = $('#zip').val();
    $.get('http:
//localhost:8000/ajax/getzipdata/' + zipcode + '/', function(data) {
      $('#city').attr({'value':data});
    });
  }
</script>
</head>
<body>
<table>
<form actioon="" method="post">
<tr><td>Zip:</td><td><input id="zip" onblur="getZipData()" type="text" name="zip"/></tr>
<tr><td>City:</td><td><input id="city" type="text" name="city"/></tr>
</form>
</table>
</body>
</html>
11. Edit views.py and add the following methods.
from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import render_to_response

def getzipdata(request, zipcode):
  
if zipcode == '214028':
    city 
= 'wuxi'
  
else:
    city 
= ''

  
return HttpResponse(city)

def register(request):
  
return render_to_response('form.html', {})
12. Add the following lines to urls.py.
(r'^ajax/register/$''study.ajax.views.register'),
(r
'^ajax/getzipdata/(\d+)/$''study.ajax.views.getzipdata'),

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.

Blogged with Flock

Tags: , , ,

posted @ 2007-11-16 14:48 django 阅读(2392) | 评论 (4)编辑 收藏
 

Fun with Idioms: in light of

In light of this new piece of evidence, I must proclaim the defendant "not guilty".

In light of latest news, we have decided to close our factories in Argentina.

Confusing Words: cure vs heal

You cannot cure the common cold.
The cut on my arm healed in a week.
Cure: To stop someone from being affected by an illness.

Heal: If an injury heals, the skin or bone grows back together and becomes healthy again.
For example:
The doctor cured the patient of his disease.
Is it possible to cure cancer?
The cut will never heals.
How long will my broken leg take to heal?

How to say it in English:

  • 这是商务仓的登机柜台么?
    • Is this the check-in counter for business class?
  • Is this where I check in for business class?
  • Does this counter server business class customers?
  • 这班班机到底会误点多少?
  • Exactly how long is this flight going to be delayed?
  • How late will this plane be?
  • Can you tell me how long the delay will be for my flight?
  • 今天所有飞北京的班机都客满了.
  • All of flights to Beijing today are full.
  • Today's flights to Beijing are completely booked.
    • There are no available seats on any flight to Beijing today.

Blogged with Flock

Tags: ,

posted @ 2007-11-15 23:05 django 阅读(273) | 评论 (0)编辑 收藏
 
This doesn't always happen.

The note is only a reminder.

She must be at least thirty-five years old.
She must be a teacher.
The student must have called you yesterday.
Darling, it must be terrible to be grown up.

It is impossible to find the thief.
It is easy to kick a man that is down.

Did you have a good meal?
Did you have a good stay?

Can I return these two tickets?
Could you come here for a moment?
Shall I take a look at the new furniture?

I might as well have them.
You might as well forget her.
You might as well come here and have a look.

Over a hundred people must have been driven away from their homes by the noisy.
The urgent notice must have been seen.

Blogged with Flock

Tags:

posted @ 2007-11-15 00:04 django 阅读(262) | 评论 (0)编辑 收藏
 
She is the cleverest girl of the six children.
He drivers fastest in his family.

We waited and waited.
The red army walked and walked.

You have belonged to me for a long time.

I am at last persuaded by him.

I was having dinner at a restaurant when Tony came in.
I was reading a newspaper when I saw a funny picture.
He was still smiling when the door opened and his wife came in.

Tony saw me and came and sat at the same table.
He opened the door and stepped on a banana skin and fell down.

He asked the guests to feel at home.
His friends all encouraged him to take the challenge.
The thief begged the policemen to let him off.
Now we are not allowed to touch it.

-Thanks for the meal. I enjoyed it very much! But I'm afraid I must be leaving.
-Not at all. I'm glad you could come. Drop in again whenever you want.
-You must come and have a meal with me some time.
-Yes. That would be nice.

Don't pretend.
Never do it again.
Never cry uncle.
Don't beat about the bush and tell us the truth.
Stop being worried about your weight.

Blogged with Flock

Tags:

posted @ 2007-11-12 21:31 django 阅读(299) | 评论 (0)编辑 收藏
 
Pinhurst is only five miles from Silbury.
My home is two miles and a half away from the company.
Guangzhou is only a fifty-minute bus-ride away from shenzhen.
The zoo is a twenty-minute walk away from here.
The United States is a long way east of China.

While two detectives were keeping guard at the door, two others opened the parcel.
While I was waiting for the train, I met my old friend by accident.
I was watching him while he was watching the kids.

To his surprise, the precious parcel was full of stones and sand.
To everybody's disappointment, the concert was a failure.
To the circus owner's pleasure, the local government presented the circus with a baby elephant called Jumbo.
To citizen's satisfaction, fireman put out the fire within 3 hours.

Generally speaking/Frankly speaking/Fortunately/To be honest/To make matters worse/To sum up

Blogged with Flock

Tags:

posted @ 2007-11-11 20:06 django 阅读(269) | 评论 (0)编辑 收藏
 
I could not bear it.
I could not bear it anymore.
It was more than I could bear it.
It is as far as I could
bear it.

It's none of your business.
Mind your own business.
Get out, f**k yourself.
Don't put your finger in my pie.

What a day!
What a beauty!
How I like the film!

From there, he will fly to Perth.
My parents used to live in South America and I used to fly there from Europe in the holidays. Just then, a man hurried to the ticket office.

So he is finding this trip very interesting.
I find him friendly.
I find his words totally meaningless.

Blogged with Flock

Tags:

posted @ 2007-11-09 00:10 django 阅读(260) | 评论 (0)编辑 收藏
 
  • Repeat last changes: "." command can be used to repeat simple text changes in normal mode.
  • Swap two characters: xp
  • Swap two lines: ddp
  • Switch case of characters:
    • ~: switch case of character under cursor
    • g~~: switch case of current line
    • gUU: make current line uppercase
    • guu: make current line lowercase
    • gUaw: make current word uppercase
    • guaw: make current word lowercase
  • Buffer management:
    • :ls list current buffers
    • :b <any part of filename> <tab> change to relative buffer
    • :b# change to the last buffer you used. Mostly, Ctrl-^ acts the same.
    • :bn change to the next buffer in the buffer list.
    • :bp change to the previous buffer in the buffer list.

Blogged with Flock

Tags:

posted @ 2007-11-08 19:55 django 阅读(347) | 评论 (0)编辑 收藏
 
  1. 在普通模式下,用*可以搜索当前光标所在的单词.
  2. 打开同目录下的文件. 普通模式下,:e,然后按tab键,选择要编辑的文件.
  3. 快速移动到代码块的开头: [{. 快速移动到代码块的结束: ]}
  4. gd会跳到当前光标下变量的声明处
  5. 把tab转换为空格:set expandtab,如果想输入真正的tab,可以用Ctrl-V<Tab>
  6. 总是显示status line: set laststatus=2
  7. 避免"Hit ENTER to continue"的提示: set shortmess=a
  8. 增加命令行的行数:set cmdheight=2
  9. 显示行数: set number
  10. 增加光标下的数字: Ctrl-A,减少光标下的数字:Ctrl-X

Blogged with Flock

Tags: ,

posted @ 2007-11-08 11:29 django 阅读(277) | 评论 (0)编辑 收藏
 
we use exchange and outlook as our email system in the company. Generally, it is fast and easy to use, except its search and tag functions. (Outlook2007 has done some enhancement, but gmail is more easy to use.)
Now gmail has opened its IMAP service for free. Here is an google article to tell us how to import local emails to gmail. Following the steps in the article, you are able to move your important local emails to gmail and enjoy gmail's powerful search and tag functions.

Blogged with Flock

Tags:

posted @ 2007-11-07 16:21 django 阅读(256) | 评论 (0)编辑 收藏
 
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.
  • 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.
  • Auto indent.
  • IPython will add parentheses to functions and methods. For example, dir '' is the same with dir('')
  • 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.
  • Dynamic object information. Type "?word" and "??word" prints detailed information about an object.
  • 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.
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.
Let's try it together!

Blogged with Flock

Tags:

posted @ 2007-11-06 16:32 django 阅读(518) | 评论 (0)编辑 收藏
仅列出标题
共12页: First 上一页 4 5 6 7 8 9 10 11 12 下一页