Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #35 from five3/master

3 change for langyage links
  • Loading branch information...
commit 5b6c1d35df0d05882ce6233291ec47a5e09cea27 2 parents 1674219 + 66ee575
@aaronsw aaronsw authored
Showing with 829 additions and 778 deletions.
  1. +264 −260 docs/0.3/tutorial.md
  2. +294 −294 install.md
  3. +271 −224 tutorial3.zh-cn.md
View
524 docs/0.3/tutorial.md
@@ -1,260 +1,264 @@
----
-layout: default
-title: Tutorial
----
-
-## Summary
-
-* [Starting](#starting)
-* [URL Handling](#urlhandling)
-* [GET and POST: the difference](#getpost)
-* [Start the server](#start)
-* [Templating](#templating)
-* [Forms](#forms)
-* [Databasing](#databasing)
-* [Developing](#developing)
-* [What next?](#whatnext)
-
-## Starting
-
-So you know Python and want to make a website. web.py provides the code to make that easy.
-
-If you want to do the whole tutorial, you'll need to have installed Python, web.py, flup, psycopg2, and Postgres (or equivalent database and Python driver). (See <a href="/install">install</a> for details.)
-
-Let's get started.
-
-<a name="urlhandling"> </a>
-## URL Handling
-
-The most important part of any website is its URL structure. Your URLs aren't just the thing that your visitors see and email to their friends, they also provide a mental model of how your website works. On popular sites like [del.icio.us](http://del.icio.us/), the URLs are even part of the user interface. web.py makes it easy to make great URLs.
-
-To get started with your web.py application, open up a new text file (let's call it `code.py`) and type:
-
- import web
-
-This imports the web.py module.
-
-Now we need to tell web.py our URL structure. Let's start out with something simple:
-
- urls = (
- '/', 'index'
- )
-
-The first part is a [regular expressions](http://osteele.com/tools/rework/) that matches a URL, like `/`, `/help/faq`, `/item/(\d+)`, etc. (i.e. `\d+` would match a sequence of digits). The parentheses say to capture that piece of the matched data for use later on. The second part is the name of a class to send the request to, like `index`, `view`, `welcomes.hello` (which gets the `hello` class of the `welcomes` module), or `get_\1`. `\1` is replaced by the first capture of your regular expression; any remaining captures get passed to your function.
-
-This line says we want the URL `/` (i.e. the front page) to be handled by the class named `index`.
-
-<a name="getpost"> </a>
-## GET and POST: the difference
-
-Now we need to write the `index` class. While most people don't notice it just browsing around, your browser uses a language known as HTTP for communicating with the World Wide Web. The details aren't important, but the basic idea is that Web visitors ask web servers to perform certain functions (like `GET` or `POST`) on URLs (like `/` or `/foo?f=1`).
-
-`GET` is the one we're all familiar with, the one used to request the text of a web page. When you type `harvard.edu` into your web browser, it literally asks the Harvard web server to `GET /`. The second-most famous, `POST`, is often used when submitting certain kinds of forms, like a request to purchase something. You use `POST` whenever the act of submitting a request _does something_ (like charge your credit card and process an order). This is key, because `GET` URLs can be passed around and indexed by search engines, which you definitely want for most of your pages but definitely _don't_ want for things like processing orders (imagine if Google tried to buy everything on your site!).
-
-In our web.py code, we make the distinction between the two clear:
-
- class index:
- def GET(self):
- return "Hello, world!"
-
-This `GET` function will now get called by web.py anytime someone makes a `GET` request for `/`.
-
-Now we need to create an application specifying the urls and a way to tell web.py to start serving web pages:
-
- if __name__ == "__main__":
- app = web.application(urls, globals())
- app.run()
-
-First we tell web.py to create an application with the URLs we listed above, looking up the classes in the global namespace of this file.
-And finally we make sure that web.py serves the application we created above.
-
-Now notice that although I've been talking a lot here, we only really have five or so lines of code. That's all you need to make a complete web.py application.
-
-For easier access, here's how your code should look like:
-
- import web
-
- urls = (
- '/', 'index'
- )
-
- class index:
- def GET(self):
- return "Hello, world!"
-
- if __name__ == "__main__":
- app = web.application(urls, globals())
- app.run()
-
-<a name="start"> </a>
-## Start the server
-
- If you go to your command line and type:
-
- $ python code.py
- http://0.0.0.0:8080/
-
-You now have your web.py application running a real web server on your computer. Visit that URL and you should see "Hello, world!" (You can add an IP address/port after the "code.py" bit to control where web.py launches the server. You can also tell it to run a `fastcgi` or `scgi` server.)
-
-**Note:** You can specify the port number to use on the command line like this
-if you can't or don't want to use the default:
-
- $ python code.py 1234
-
-
-<a name="templating"> </a>
-## Templating
-
-Writing HTML from inside Python can get cumbersome; it's much more fun to write Python from inside HTML. Luckily, web.py makes that pretty easy.
-
-Let's make a new directory for our templates (we'll call it `templates`). Inside, make a new file whose name ends with HTML (we'll call it `index.html`). Now, inside, you can just write normal HTML:
-
- <em>Hello</em>, world!
-
-Or you can use web.py's templating language to add code to your HTML:
-
- $def with (name)
-
- $if name:
- I just wanted to say <em>hello</em> to $name.
- $else:
- <em>Hello</em>, world!
-
-As you can see, the templates look a lot like Python files except for the `def with` statement at the top (saying what the template gets called with) and the `$`s placed in front of any code. Currently, template.py requires the `$def` statement to be the first line of the file. Also, note that web.py automatically escapes any variables used here, so that if for some reason `name` is set to a value containing some HTML, it will get properly escaped and appear as plain text. If you want to turn this off, write `$:name` instead of `$name`.
-
-Now go back to `code.py`. Under the first line, add:
-
- render = web.template.render('templates/')
-
-This tells web.py to look for templates in your templates directory. Then change `index.GET` to:
-
- name = 'Bob'
- return render.index(name)
-
-('index' is the name of the template and 'name' is the argument passed to it)
-
-Visit your site and it should say hello to Bob.
-
-But let's say we want to let people enter their own name in. Replace the two lines we added above with:
-
- i = web.input(name=None)
- return render.index(i.name)
-
-Visit `/` and it should say hello to the world. Visit `/?name=Joe` and it should say hello to Joe.
-
-Of course, having that `?` in the URL is kind of ugly. Instead, change your URL line at the top to:
-
- '/(.*)', 'index'
-
-and change the definition of `index.GET` to:
-
- def GET(self, name):
- return render.index(name)
-
-and delete the line setting name. Now visit `/Joe` and it should say hello to Joe.
-
-If you wish to learn more about web.py templates, vist the <a href="/docs/0.3/templetor">templetor page</a>.
-
-<a name="forms"> </a>
-## Forms
-
-The form module of web.py allows the ability to generate html forms, get user input, and validate it before processing it or adding it to a database.
-If you want to learn more about using the module forms web.py, see the [Documentation](/docs/0.3) or direct link to [Form Library](/form)
-
-<a name="databasing"> </a>
-## Databasing
-
-**Note:** Before you can start using a database, make sure you have the appropriate database library installed. For MySQL databases, use [MySQLdb](http://sourceforge.net/project/showfiles.php?group_id=22307) and for Postgres use [psycopg2](http://initd.org/pub/software/psycopg/).
-
-First you need to create a database object.
-
- db = web.database(dbn='postgres', user='username', pw='password', db='dbname')
-
-(Adjust these -- especially `username`, `password`, and `dbname` -- for your setup. MySQL users will also want to change `dbn` definition to `mysql`.)
-
-That's all you need to do -- web.py will automatically handle connecting and disconnecting from the database.
-
-Using your database engines admin interface, create a simple table in your database:
-
- CREATE TABLE todo (
- id serial primary key,
- title text,
- created timestamp default now(),
- done boolean default 'f' );
-
-And an initial row:
-
- INSERT INTO todo (title) VALUES ('Learn web.py');
-
-Return to editing `code.py` and change `index.GET` to the following, replacing the entire function:
-
- def GET(self):
- todos = db.select('todo')
- return render.index(todos)
-
-and change back the URL handler to take just `/` as in:
-
- '/', 'index',
-
-Edit and replace the entire contents of `index.html` so that it reads:
-
- $def with (todos)
- <ul>
- $for todo in todos:
- <li id="t$todo.id">$todo.title</li>
- </ul>
-
-Visit your site again and you should see your one todo item: "Learn web.py". Congratulations! You've made a full application that reads from the database. Now let's let it write to the database as well.
-
-At the end of `index.html`, add:
-
- <form method="post" action="add">
- <p><input type="text" name="title" /> <input type="submit" value="Add" /></p>
- </form>
-
-And change your URLs list to read:
-
- '/', 'index',
- '/add', 'add'
-
-(You've got to be very careful about those commas. If you omit them, Python adds the strings together and sees `'/index/addadd'` instead of your list of URLs!)
-
-Now add another class:
-
- class add:
- def POST(self):
- i = web.input()
- n = db.insert('todo', title=i.title)
- raise web.seeother('/')
-
-(Notice how we're using `POST` for this?)
-
-`web.input` gives you access to any variables the user submitted through a form.
-
-Note: In order to access data from multiple identically-named items, in a list format (e.g.: a series of check-boxes all with the attribute name="name") use:
-
- post_data=web.input(name=[])
-
-`db.insert` inserts values into the database table `todo` and gives you back the ID of the new row. `seeother` redirects users to that URL.
-
-Some quick additional notes: `db.update` works just like `db.insert` except instead of returning the ID it takes it (or a string `WHERE` clause) after the table name.
-
-`web.input`, `db.query`, and other functions in web.py return "Storage objects", which are just like dictionaries except you can do `d.foo` in addition to `d['foo']`. This really cleans up some code.
-
-You can find the full details on these and all the web.py functions in [the documentation](/docs/0.3).
-
-<a name="developing"> </a>
-## Developing
-
-web.py also has a few tools to help us with debugging. When running with the built-in webserver, it starts the application in debug mode. In debug mode any changes to code and templates are automatically reloaded and error messages will have more helpful information.
-
-The debug is not enabled when the application is run in a real webserver. If you want to disable the debug mode, you can do so by adding the following line before creating your application/templates.
-
- web.config.debug = False
-
-<a name="whatnext"> </a>
-## What Next ?
-
-This ends the tutorial for now. Take a look at the [cookbook](/cookbook/) and the [code examples](/src/) for lots more cool stuff you can do with web.py.
-Also don't forget about the [api reference](/docs/0.3/api)
-
+---
+layout: default
+title: Tutorial
+---
+
+# Tutorial
+
+Other languages : [chinese 简体中文 ](/tutorial3.zh-cn) | [français](/tutorial3.fr) | ...
+
+## Summary
+
+* [Starting](#starting)
+* [URL Handling](#urlhandling)
+* [GET and POST: the difference](#getpost)
+* [Start the server](#start)
+* [Templating](#templating)
+* [Forms](#forms)
+* [Databasing](#databasing)
+* [Developing](#developing)
+* [What next?](#whatnext)
+
+## Starting
+
+So you know Python and want to make a website. web.py provides the code to make that easy.
+
+If you want to do the whole tutorial, you'll need to have installed Python, web.py, flup, psycopg2, and Postgres (or equivalent database and Python driver). (See <a href="/install">install</a> for details.)
+
+Let's get started.
+
+<a name="urlhandling"> </a>
+## URL Handling
+
+The most important part of any website is its URL structure. Your URLs aren't just the thing that your visitors see and email to their friends, they also provide a mental model of how your website works. On popular sites like [del.icio.us](http://del.icio.us/), the URLs are even part of the user interface. web.py makes it easy to make great URLs.
+
+To get started with your web.py application, open up a new text file (let's call it `code.py`) and type:
+
+ import web
+
+This imports the web.py module.
+
+Now we need to tell web.py our URL structure. Let's start out with something simple:
+
+ urls = (
+ '/', 'index'
+ )
+
+The first part is a [regular expressions](http://osteele.com/tools/rework/) that matches a URL, like `/`, `/help/faq`, `/item/(\d+)`, etc. (i.e. `\d+` would match a sequence of digits). The parentheses say to capture that piece of the matched data for use later on. The second part is the name of a class to send the request to, like `index`, `view`, `welcomes.hello` (which gets the `hello` class of the `welcomes` module), or `get_\1`. `\1` is replaced by the first capture of your regular expression; any remaining captures get passed to your function.
+
+This line says we want the URL `/` (i.e. the front page) to be handled by the class named `index`.
+
+<a name="getpost"> </a>
+## GET and POST: the difference
+
+Now we need to write the `index` class. While most people don't notice it just browsing around, your browser uses a language known as HTTP for communicating with the World Wide Web. The details aren't important, but the basic idea is that Web visitors ask web servers to perform certain functions (like `GET` or `POST`) on URLs (like `/` or `/foo?f=1`).
+
+`GET` is the one we're all familiar with, the one used to request the text of a web page. When you type `harvard.edu` into your web browser, it literally asks the Harvard web server to `GET /`. The second-most famous, `POST`, is often used when submitting certain kinds of forms, like a request to purchase something. You use `POST` whenever the act of submitting a request _does something_ (like charge your credit card and process an order). This is key, because `GET` URLs can be passed around and indexed by search engines, which you definitely want for most of your pages but definitely _don't_ want for things like processing orders (imagine if Google tried to buy everything on your site!).
+
+In our web.py code, we make the distinction between the two clear:
+
+ class index:
+ def GET(self):
+ return "Hello, world!"
+
+This `GET` function will now get called by web.py anytime someone makes a `GET` request for `/`.
+
+Now we need to create an application specifying the urls and a way to tell web.py to start serving web pages:
+
+ if __name__ == "__main__":
+ app = web.application(urls, globals())
+ app.run()
+
+First we tell web.py to create an application with the URLs we listed above, looking up the classes in the global namespace of this file.
+And finally we make sure that web.py serves the application we created above.
+
+Now notice that although I've been talking a lot here, we only really have five or so lines of code. That's all you need to make a complete web.py application.
+
+For easier access, here's how your code should look like:
+
+ import web
+
+ urls = (
+ '/', 'index'
+ )
+
+ class index:
+ def GET(self):
+ return "Hello, world!"
+
+ if __name__ == "__main__":
+ app = web.application(urls, globals())
+ app.run()
+
+<a name="start"> </a>
+## Start the server
+
+ If you go to your command line and type:
+
+ $ python code.py
+ http://0.0.0.0:8080/
+
+You now have your web.py application running a real web server on your computer. Visit that URL and you should see "Hello, world!" (You can add an IP address/port after the "code.py" bit to control where web.py launches the server. You can also tell it to run a `fastcgi` or `scgi` server.)
+
+**Note:** You can specify the port number to use on the command line like this
+if you can't or don't want to use the default:
+
+ $ python code.py 1234
+
+
+<a name="templating"> </a>
+## Templating
+
+Writing HTML from inside Python can get cumbersome; it's much more fun to write Python from inside HTML. Luckily, web.py makes that pretty easy.
+
+Let's make a new directory for our templates (we'll call it `templates`). Inside, make a new file whose name ends with HTML (we'll call it `index.html`). Now, inside, you can just write normal HTML:
+
+ <em>Hello</em>, world!
+
+Or you can use web.py's templating language to add code to your HTML:
+
+ $def with (name)
+
+ $if name:
+ I just wanted to say <em>hello</em> to $name.
+ $else:
+ <em>Hello</em>, world!
+
+As you can see, the templates look a lot like Python files except for the `def with` statement at the top (saying what the template gets called with) and the `$`s placed in front of any code. Currently, template.py requires the `$def` statement to be the first line of the file. Also, note that web.py automatically escapes any variables used here, so that if for some reason `name` is set to a value containing some HTML, it will get properly escaped and appear as plain text. If you want to turn this off, write `$:name` instead of `$name`.
+
+Now go back to `code.py`. Under the first line, add:
+
+ render = web.template.render('templates/')
+
+This tells web.py to look for templates in your templates directory. Then change `index.GET` to:
+
+ name = 'Bob'
+ return render.index(name)
+
+('index' is the name of the template and 'name' is the argument passed to it)
+
+Visit your site and it should say hello to Bob.
+
+But let's say we want to let people enter their own name in. Replace the two lines we added above with:
+
+ i = web.input(name=None)
+ return render.index(i.name)
+
+Visit `/` and it should say hello to the world. Visit `/?name=Joe` and it should say hello to Joe.
+
+Of course, having that `?` in the URL is kind of ugly. Instead, change your URL line at the top to:
+
+ '/(.*)', 'index'
+
+and change the definition of `index.GET` to:
+
+ def GET(self, name):
+ return render.index(name)
+
+and delete the line setting name. Now visit `/Joe` and it should say hello to Joe.
+
+If you wish to learn more about web.py templates, vist the <a href="/docs/0.3/templetor">templetor page</a>.
+
+<a name="forms"> </a>
+## Forms
+
+The form module of web.py allows the ability to generate html forms, get user input, and validate it before processing it or adding it to a database.
+If you want to learn more about using the module forms web.py, see the [Documentation](/docs/0.3) or direct link to [Form Library](/form)
+
+<a name="databasing"> </a>
+## Databasing
+
+**Note:** Before you can start using a database, make sure you have the appropriate database library installed. For MySQL databases, use [MySQLdb](http://sourceforge.net/project/showfiles.php?group_id=22307) and for Postgres use [psycopg2](http://initd.org/pub/software/psycopg/).
+
+First you need to create a database object.
+
+ db = web.database(dbn='postgres', user='username', pw='password', db='dbname')
+
+(Adjust these -- especially `username`, `password`, and `dbname` -- for your setup. MySQL users will also want to change `dbn` definition to `mysql`.)
+
+That's all you need to do -- web.py will automatically handle connecting and disconnecting from the database.
+
+Using your database engines admin interface, create a simple table in your database:
+
+ CREATE TABLE todo (
+ id serial primary key,
+ title text,
+ created timestamp default now(),
+ done boolean default 'f' );
+
+And an initial row:
+
+ INSERT INTO todo (title) VALUES ('Learn web.py');
+
+Return to editing `code.py` and change `index.GET` to the following, replacing the entire function:
+
+ def GET(self):
+ todos = db.select('todo')
+ return render.index(todos)
+
+and change back the URL handler to take just `/` as in:
+
+ '/', 'index',
+
+Edit and replace the entire contents of `index.html` so that it reads:
+
+ $def with (todos)
+ <ul>
+ $for todo in todos:
+ <li id="t$todo.id">$todo.title</li>
+ </ul>
+
+Visit your site again and you should see your one todo item: "Learn web.py". Congratulations! You've made a full application that reads from the database. Now let's let it write to the database as well.
+
+At the end of `index.html`, add:
+
+ <form method="post" action="add">
+ <p><input type="text" name="title" /> <input type="submit" value="Add" /></p>
+ </form>
+
+And change your URLs list to read:
+
+ '/', 'index',
+ '/add', 'add'
+
+(You've got to be very careful about those commas. If you omit them, Python adds the strings together and sees `'/index/addadd'` instead of your list of URLs!)
+
+Now add another class:
+
+ class add:
+ def POST(self):
+ i = web.input()
+ n = db.insert('todo', title=i.title)
+ raise web.seeother('/')
+
+(Notice how we're using `POST` for this?)
+
+`web.input` gives you access to any variables the user submitted through a form.
+
+Note: In order to access data from multiple identically-named items, in a list format (e.g.: a series of check-boxes all with the attribute name="name") use:
+
+ post_data=web.input(name=[])
+
+`db.insert` inserts values into the database table `todo` and gives you back the ID of the new row. `seeother` redirects users to that URL.
+
+Some quick additional notes: `db.update` works just like `db.insert` except instead of returning the ID it takes it (or a string `WHERE` clause) after the table name.
+
+`web.input`, `db.query`, and other functions in web.py return "Storage objects", which are just like dictionaries except you can do `d.foo` in addition to `d['foo']`. This really cleans up some code.
+
+You can find the full details on these and all the web.py functions in [the documentation](/docs/0.3).
+
+<a name="developing"> </a>
+## Developing
+
+web.py also has a few tools to help us with debugging. When running with the built-in webserver, it starts the application in debug mode. In debug mode any changes to code and templates are automatically reloaded and error messages will have more helpful information.
+
+The debug is not enabled when the application is run in a real webserver. If you want to disable the debug mode, you can do so by adding the following line before creating your application/templates.
+
+ web.config.debug = False
+
+<a name="whatnext"> </a>
+## What Next ?
+
+This ends the tutorial for now. Take a look at the [cookbook](/cookbook/) and the [code examples](/src/) for lots more cool stuff you can do with web.py.
+Also don't forget about the [api reference](/docs/0.3/api)
+
View
588 install.md
@@ -1,294 +1,294 @@
----
-layout: default
-title: Install guide
----
-
-# Install guide
-
-Other languages : [español](/install/es) | [Japan 日本語 ](/install/ja) | [chinese 简体中文 ](/install/zh-cn) | [italiano](/install/it) | [français](/install/fr)
-
-## Summary
-
-* <a href="#install">Install</a>
- * <a href="#macosx">.. on MacOS X</a>
-* <a href="#dev">Development</a>
-* <a href="#prod">Production</a>
- * <a href="#lighttpd">LightTPD</a>
- * <a href="#lighttpdfastcgi">.. with FastCGI</a>
- * <a href="#apache">Apache</a>
- * <a href="#apachecgi">.. with CGI</a>
- * <a href="#apachecgihtaccess"> .. with CGI using .htaccess</a>
- * <a href="#apachefastcgi">.. with FastCGI</a>
- * <a href="#apachescgi">.. with SCGI</a>
- * <a href="#apachemodpython">.. with mod_python</a>
- * <a href="#apachemodwsgi">.. with mod_wsgi</a>
- * <a href="#apachemodrewrite">.. with mod_rewrite</a>
-
-
-<a name="install"></a>
-## Install
-
-
-To install web.py, download:
-
- http://webpy.org/static/web.py-0.37.tar.gz
-
-or the get the latest dev version:
-
- https://github.com/webpy/webpy/tarball/master
-
-extract it and copy the _web_ folder into a directory where your application is. Or, to make it accessible to all applications, run:
-
- python setup.py install
-
-Note: on some unix like systems you may need to switch to root or run:
-
- sudo python setup.py install
-
-see [recommended setup](/recommended_setup).
-
-Another option is to use [Easy Install](http://peak.telecommunity.com/DevCenter/EasyInstall). Once Easy Install is properly setup:
-
-
- sudo easy_install web.py
-
-Or [PIP](http://packages.python.org/distribute/)
-
- sudo pip install web.py
-
-<a name="macosx"></a>
-### MacOS X
-
-To install web.py on [mac os x](/install_macosx). Web.py 0.1 only...
-
-<a name="dev"></a>
-## Development
-
-web.py comes with a built-in webserver. Learn how to write an application by following the [tutorial](http://webpy.org/tutorial). When you have an application written, put your code into `code.py` and start the server like this:
-
- python code.py
-
-Open your browser and go to [http://localhost:8080/](http://localhost:8080/) to view the page. To specify another port, use `python code.py 1234`.
-
-<a name="prod"></a>
-## Production
-
-The web server that gets started when you run a web.py program is nice, but for popular sites you're going to want something a little more serious. web.py implements [WSGI](http://www.python.org/dev/peps/pep-0333/) and runs with everything that is compatible to it. WSGI is a common API between web servers and applications, analogous to Java's Servlet Interface. To run web.py with CGI, FastCGI or SCGI you will need to install [flup](http://trac.saddi.com/flup) ([download here](http://www.saddi.com/software/flup/dist/)), which provides WSGI interfaces for those APIs.
-
-For all the CGI variants, add this to the top of your `code.py`:
-
- #!/usr/bin/env python
-
-And run `chmod +x code.py` to make it executable.
-
-<a name="lighttpd"></a>
-### LightTPD
-
-<a name="lighttpdfastcgi"></a>
-#### .. with FastCGI
-
-FastCGI with lighttpd is the recommended way of using web.py in production. [reddit.com][3] handles millions of hits this way.
-
- [3]: http://reddit.com/
-
-Your lighttpd config can be something like:
-
- server.modules = ("mod_fastcgi", "mod_rewrite")
- server.document-root = "/path/to/root/"
- fastcgi.server = ( "/code.py" =>
- (( "socket" => "/tmp/fastcgi.socket",
- "bin-path" => "/path/to/root/code.py",
- "max-procs" => 1
- ))
- )
-
- url.rewrite-once = (
- "^/favicon.ico$" => "/static/favicon.ico",
- "^/static/(.*)$" => "/static/$1",
- "^/(.*)$" => "/code.py/$1"
- )
-
-With some versions of lighttpd, it is important to ensure the "check-local" property of the fastcgi.server entry is set to "false", especially if your `code.py` is located outside of the document root.
-
-If you get error messages about not being able to import flup, install it by typing "easy_install flup" at the command line.
-
-Since revision 145, it is necessary to set a bin-environment variable on the fastcgi configuration if your code uses redirects. If when your code redirects to http://domain.com/ and in the url bar you see http://domain.com/code.py/, you'll need to set the environment variable. This will cause your fastcgi.server configuration above to look something like this:
-
- fastcgi.server = ( "/code.py" =>
- ((
- "socket" => "/tmp/fastcgi.socket",
- "bin-path" => "/path/to/root/code.py",
- "max-procs" => 1,
- "bin-environment" => (
- "REAL_SCRIPT_NAME" => ""
- ),
- "check-local" => "disable"
- ))
- )
-
-<a name="apache"></a>
-### Apache
-
-<a name="apachecgi"></a>
-#### .. with CGI
-
-
-Add the following to `httpd.conf` or `apache2.conf`.
-
- Alias /foo/static/ /path/to/static
- ScriptAlias /foo/ /path/to/code.py
-
-<a name="apachecgihtaccess"></a>
-#### .. with CGI using .htaccess
-
-CGI is easy to configure, but is not suitable for high-performance websites.
-Add this to your `.htaccess`:
-
- Options +ExecCGI
- AddHandler cgi-script .py
-
-and point your browser to `http://example.com/code.py/`. Don't forget the trailing slash, otherwise you'll see a `not found` message (because the `urls` list you defined do not match anything). To make things work without having to enter `code.py`, enable mod_rewrite rules (see below).
-
-Note: The way `web.py` is implemented breaks the `cgitb` module because it captures `stdout`. I worked around the issue by using this:
-
- import cgitb; cgitb.enable()
- import sys
-
- # ... import web etc here...
-
- def cgidebugerror():
- """
- """
- _wrappedstdout = sys.stdout
-
- sys.stdout = web._oldstdout
- cgitb.handler()
-
- sys.stdout = _wrappedstdout
-
- web.internalerror = cgidebugerror
-
-
-<a name="apachefastcgi"></a>
-#### .. with FastCGI
-
-FastCGI is easy to configure and performs as well as mod_python.
-
-Add this to your `.htaccess`:
-
- <Files code.py>
- SetHandler fastcgi-script
- </Files>
-
-Unfortunately, unlike lighttpd, Apache gives no hint that it wants your web.py script to act as a FastCGI server so you have to tell web.py explicitly. Add this to `code.py` before your `if __name__ == "__main__":` line:
-
- web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
-
-and point your browser to `http://example.com/code.py/`. Don't forget the trailing slash, otherwise you'll see a `not found` message (because the `urls` list you defined do not match anything). To make things work without having to enter `code.py`, enable mod_rewrite rules (see below).
-
-[Walter has some additional advice](http://lemurware.blogspot.com/2006/05/webpy-apache-configuration-and-you.html).
-
-
-<a name="apachescgi"></a>
-#### .. with SCGI
-https://www.mems-exchange.org/software/scgi/
-download `mod_scgi` source here: http://www.mems-exchange.org/software/files/mod_scgi/
-windows apache user:
-edit your httpd.conf:
-
- LoadModule scgi_module Modules/mod_scgi.so
- SCGIMount / 127.0.0.1:8080
-
-restart apache and then start your code.py in the command below:
-
- python code.py 127.0.0.1:8080 scgi
-
-and open you browser,visit 127.0.0.1
-It's ok!
-
-
-<a name="apachemodpython"></a>
-#### .. with mod_python
-
-mod_python performs as well as FastCGI, but is not as straight-forward to configure.
-
-For Python 2.5 do this:
-
- cd /usr/lib/python2.5/wsgiref
- # or in windows: cd /python2.5/lib/wsgiref
- wget -O modpython_gateway.py http://svn.aminus.net/misc/modpython_gateway.py
- # or fetch the file from that address using your browser
-
-For Python <2.5 do this:
-
- cd /usr/lib/python2.4/site-packages
- # or in windows: cd /python2.4/lib/site-packages
- svn co svn://svn.eby-sarna.com/svnroot/wsgiref/wsgiref
- cd wsgiref
- wget -O modpython_gateway.py http://svn.aminus.net/misc/modpython_gateway.py
- # or fetch the file from that address using your browser
-
-
-Rename your `code.py` to something like `codep.py` and add:
-
- app = web.application(urls, globals())
- main = app.wsgifunc()
-
-In your `.htaccess`, add:
-
-
- AddHandler python-program .py
- PythonHandler wsgiref.modpython_gateway::handler
- PythonOption wsgi.application codep::main
-
-
-You also probably want to add a `RewriteRule` pointing `/` to `/codep.py/`
-
-Be sure to visit `/codep.py/` with the extra `/` on the end. Otherwise, you'll see an error message like `A server error occurred. Please contact the administrator.`
-
-<a name="apachemodwsgi"></a>
-#### .. with mod_wsgi
-
-mod\_wsgi is a new Apache module which [typically outperforms mod_python](http://code.google.com/p/modwsgi/wiki/PerformanceEstimates) for hosting WSGI applications, and is very easy to set up. <div style="text-indent:-90000px;">[Watch Live Sports Online](http://www.sportspiks.com)[Watch tv episodes online](http://purehunting.blogspot.com)[Watch movies Online](http://www.overheadmovies.com)[Watch Resident Evil Afterlife (2010) Online](http://www.overheadmovies.com/action/resident-evil-afterlife-2010)</div>
-
-At the end of your `code.py`, add:
-
- app = web.application(urls, globals(), autoreload=False)
- application = app.wsgifunc()
-
-mod\_wsgi offers [many possible ways](http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives) to expose a WSGI application in Apache's URL hierarchy, but one simple way would be to add the following to your .htaccess:
-
- <Files code.py>
- SetHandler wsgi-script
- Options ExecCGI FollowSymLinks
- </Files>
-
-If you get an "ImportError: No module named web" in your apache error.log file, you could try setting the absolute path in code.py before importing web:
-
- import sys, os
- abspath = os.path.dirname(__file__)
- sys.path.append(abspath)
- os.chdir(abspath)
- import web
-
-Also, you might want to read the "Application Working Directory" section from [Common problems with WSGI application](http://code.google.com/p/modwsgi/wiki/ApplicationIssues).
-
-It should then be accessible at `http://example.com/code.py/` as usual.
-
-
-
-<a name="apachemodrewrite"></a>
-#### mod_rewrite Rules for Apache
-
-If you want web.py to be accessible at 'http://example.com' instead of 'http://example.com/code.py/' add the following rules to the `.htaccess` file:
-
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteBase /
- RewriteCond %{REQUEST_URI} !^/icons
- RewriteCond %{REQUEST_URI} !^/favicon.ico$
- RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
- RewriteRule ^(.*)$ code.py/$1 [PT]
- </IfModule>
-
-If the `code.py` is in the subfolder `myapp/`, adjust the RewriteBase to `RewriteBase /myapp/`. If you have static files like CSS files and images to pass through, duplicate the line with the icons for each path you want to allow.
+---
+layout: default
+title: Install guide
+---
+
+# Install guide
+
+Other languages : [español](/install.es) | [Japan 日本語 ](/install.ja) | [chinese 简体中文 ](/install.zh-cn) | [italiano](/install.it) | [français](/install.fr)
+
+## Summary
+
+* <a href="#install">Install</a>
+ * <a href="#macosx">.. on MacOS X</a>
+* <a href="#dev">Development</a>
+* <a href="#prod">Production</a>
+ * <a href="#lighttpd">LightTPD</a>
+ * <a href="#lighttpdfastcgi">.. with FastCGI</a>
+ * <a href="#apache">Apache</a>
+ * <a href="#apachecgi">.. with CGI</a>
+ * <a href="#apachecgihtaccess"> .. with CGI using .htaccess</a>
+ * <a href="#apachefastcgi">.. with FastCGI</a>
+ * <a href="#apachescgi">.. with SCGI</a>
+ * <a href="#apachemodpython">.. with mod_python</a>
+ * <a href="#apachemodwsgi">.. with mod_wsgi</a>
+ * <a href="#apachemodrewrite">.. with mod_rewrite</a>
+
+
+<a name="install"></a>
+## Install
+
+
+To install web.py, download:
+
+ http://webpy.org/static/web.py-0.37.tar.gz
+
+or the get the latest dev version:
+
+ https://github.com/webpy/webpy/tarball/master
+
+extract it and copy the _web_ folder into a directory where your application is. Or, to make it accessible to all applications, run:
+
+ python setup.py install
+
+Note: on some unix like systems you may need to switch to root or run:
+
+ sudo python setup.py install
+
+see [recommended setup](/recommended_setup).
+
+Another option is to use [Easy Install](http://peak.telecommunity.com/DevCenter/EasyInstall). Once Easy Install is properly setup:
+
+
+ sudo easy_install web.py
+
+Or [PIP](http://packages.python.org/distribute/)
+
+ sudo pip install web.py
+
+<a name="macosx"></a>
+### MacOS X
+
+To install web.py on [mac os x](/install_macosx). Web.py 0.1 only...
+
+<a name="dev"></a>
+## Development
+
+web.py comes with a built-in webserver. Learn how to write an application by following the [tutorial](http://webpy.org/tutorial). When you have an application written, put your code into `code.py` and start the server like this:
+
+ python code.py
+
+Open your browser and go to [http://localhost:8080/](http://localhost:8080/) to view the page. To specify another port, use `python code.py 1234`.
+
+<a name="prod"></a>
+## Production
+
+The web server that gets started when you run a web.py program is nice, but for popular sites you're going to want something a little more serious. web.py implements [WSGI](http://www.python.org/dev/peps/pep-0333/) and runs with everything that is compatible to it. WSGI is a common API between web servers and applications, analogous to Java's Servlet Interface. To run web.py with CGI, FastCGI or SCGI you will need to install [flup](http://trac.saddi.com/flup) ([download here](http://www.saddi.com/software/flup/dist/)), which provides WSGI interfaces for those APIs.
+
+For all the CGI variants, add this to the top of your `code.py`:
+
+ #!/usr/bin/env python
+
+And run `chmod +x code.py` to make it executable.
+
+<a name="lighttpd"></a>
+### LightTPD
+
+<a name="lighttpdfastcgi"></a>
+#### .. with FastCGI
+
+FastCGI with lighttpd is the recommended way of using web.py in production. [reddit.com][3] handles millions of hits this way.
+
+ [3]: http://reddit.com/
+
+Your lighttpd config can be something like:
+
+ server.modules = ("mod_fastcgi", "mod_rewrite")
+ server.document-root = "/path/to/root/"
+ fastcgi.server = ( "/code.py" =>
+ (( "socket" => "/tmp/fastcgi.socket",
+ "bin-path" => "/path/to/root/code.py",
+ "max-procs" => 1
+ ))
+ )
+
+ url.rewrite-once = (
+ "^/favicon.ico$" => "/static/favicon.ico",
+ "^/static/(.*)$" => "/static/$1",
+ "^/(.*)$" => "/code.py/$1"
+ )
+
+With some versions of lighttpd, it is important to ensure the "check-local" property of the fastcgi.server entry is set to "false", especially if your `code.py` is located outside of the document root.
+
+If you get error messages about not being able to import flup, install it by typing "easy_install flup" at the command line.
+
+Since revision 145, it is necessary to set a bin-environment variable on the fastcgi configuration if your code uses redirects. If when your code redirects to http://domain.com/ and in the url bar you see http://domain.com/code.py/, you'll need to set the environment variable. This will cause your fastcgi.server configuration above to look something like this:
+
+ fastcgi.server = ( "/code.py" =>
+ ((
+ "socket" => "/tmp/fastcgi.socket",
+ "bin-path" => "/path/to/root/code.py",
+ "max-procs" => 1,
+ "bin-environment" => (
+ "REAL_SCRIPT_NAME" => ""
+ ),
+ "check-local" => "disable"
+ ))
+ )
+
+<a name="apache"></a>
+### Apache
+
+<a name="apachecgi"></a>
+#### .. with CGI
+
+
+Add the following to `httpd.conf` or `apache2.conf`.
+
+ Alias /foo/static/ /path/to/static
+ ScriptAlias /foo/ /path/to/code.py
+
+<a name="apachecgihtaccess"></a>
+#### .. with CGI using .htaccess
+
+CGI is easy to configure, but is not suitable for high-performance websites.
+Add this to your `.htaccess`:
+
+ Options +ExecCGI
+ AddHandler cgi-script .py
+
+and point your browser to `http://example.com/code.py/`. Don't forget the trailing slash, otherwise you'll see a `not found` message (because the `urls` list you defined do not match anything). To make things work without having to enter `code.py`, enable mod_rewrite rules (see below).
+
+Note: The way `web.py` is implemented breaks the `cgitb` module because it captures `stdout`. I worked around the issue by using this:
+
+ import cgitb; cgitb.enable()
+ import sys
+
+ # ... import web etc here...
+
+ def cgidebugerror():
+ """
+ """
+ _wrappedstdout = sys.stdout
+
+ sys.stdout = web._oldstdout
+ cgitb.handler()
+
+ sys.stdout = _wrappedstdout
+
+ web.internalerror = cgidebugerror
+
+
+<a name="apachefastcgi"></a>
+#### .. with FastCGI
+
+FastCGI is easy to configure and performs as well as mod_python.
+
+Add this to your `.htaccess`:
+
+ <Files code.py>
+ SetHandler fastcgi-script
+ </Files>
+
+Unfortunately, unlike lighttpd, Apache gives no hint that it wants your web.py script to act as a FastCGI server so you have to tell web.py explicitly. Add this to `code.py` before your `if __name__ == "__main__":` line:
+
+ web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
+
+and point your browser to `http://example.com/code.py/`. Don't forget the trailing slash, otherwise you'll see a `not found` message (because the `urls` list you defined do not match anything). To make things work without having to enter `code.py`, enable mod_rewrite rules (see below).
+
+[Walter has some additional advice](http://lemurware.blogspot.com/2006/05/webpy-apache-configuration-and-you.html).
+
+
+<a name="apachescgi"></a>
+#### .. with SCGI
+https://www.mems-exchange.org/software/scgi/
+download `mod_scgi` source here: http://www.mems-exchange.org/software/files/mod_scgi/
+windows apache user:
+edit your httpd.conf:
+
+ LoadModule scgi_module Modules/mod_scgi.so
+ SCGIMount / 127.0.0.1:8080
+
+restart apache and then start your code.py in the command below:
+
+ python code.py 127.0.0.1:8080 scgi
+
+and open you browser,visit 127.0.0.1
+It's ok!
+
+
+<a name="apachemodpython"></a>
+#### .. with mod_python
+
+mod_python performs as well as FastCGI, but is not as straight-forward to configure.
+
+For Python 2.5 do this:
+
+ cd /usr/lib/python2.5/wsgiref
+ # or in windows: cd /python2.5/lib/wsgiref
+ wget -O modpython_gateway.py http://svn.aminus.net/misc/modpython_gateway.py
+ # or fetch the file from that address using your browser
+
+For Python <2.5 do this:
+
+ cd /usr/lib/python2.4/site-packages
+ # or in windows: cd /python2.4/lib/site-packages
+ svn co svn://svn.eby-sarna.com/svnroot/wsgiref/wsgiref
+ cd wsgiref
+ wget -O modpython_gateway.py http://svn.aminus.net/misc/modpython_gateway.py
+ # or fetch the file from that address using your browser
+
+
+Rename your `code.py` to something like `codep.py` and add:
+
+ app = web.application(urls, globals())
+ main = app.wsgifunc()
+
+In your `.htaccess`, add:
+
+
+ AddHandler python-program .py
+ PythonHandler wsgiref.modpython_gateway::handler
+ PythonOption wsgi.application codep::main
+
+
+You also probably want to add a `RewriteRule` pointing `/` to `/codep.py/`
+
+Be sure to visit `/codep.py/` with the extra `/` on the end. Otherwise, you'll see an error message like `A server error occurred. Please contact the administrator.`
+
+<a name="apachemodwsgi"></a>
+#### .. with mod_wsgi
+
+mod\_wsgi is a new Apache module which [typically outperforms mod_python](http://code.google.com/p/modwsgi/wiki/PerformanceEstimates) for hosting WSGI applications, and is very easy to set up. <div style="text-indent:-90000px;">[Watch Live Sports Online](http://www.sportspiks.com)[Watch tv episodes online](http://purehunting.blogspot.com)[Watch movies Online](http://www.overheadmovies.com)[Watch Resident Evil Afterlife (2010) Online](http://www.overheadmovies.com/action/resident-evil-afterlife-2010)</div>
+
+At the end of your `code.py`, add:
+
+ app = web.application(urls, globals(), autoreload=False)
+ application = app.wsgifunc()
+
+mod\_wsgi offers [many possible ways](http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives) to expose a WSGI application in Apache's URL hierarchy, but one simple way would be to add the following to your .htaccess:
+
+ <Files code.py>
+ SetHandler wsgi-script
+ Options ExecCGI FollowSymLinks
+ </Files>
+
+If you get an "ImportError: No module named web" in your apache error.log file, you could try setting the absolute path in code.py before importing web:
+
+ import sys, os
+ abspath = os.path.dirname(__file__)
+ sys.path.append(abspath)
+ os.chdir(abspath)
+ import web
+
+Also, you might want to read the "Application Working Directory" section from [Common problems with WSGI application](http://code.google.com/p/modwsgi/wiki/ApplicationIssues).
+
+It should then be accessible at `http://example.com/code.py/` as usual.
+
+
+
+<a name="apachemodrewrite"></a>
+#### mod_rewrite Rules for Apache
+
+If you want web.py to be accessible at 'http://example.com' instead of 'http://example.com/code.py/' add the following rules to the `.htaccess` file:
+
+ <IfModule mod_rewrite.c>
+ RewriteEngine on
+ RewriteBase /
+ RewriteCond %{REQUEST_URI} !^/icons
+ RewriteCond %{REQUEST_URI} !^/favicon.ico$
+ RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
+ RewriteRule ^(.*)$ code.py/$1 [PT]
+ </IfModule>
+
+If the `code.py` is in the subfolder `myapp/`, adjust the RewriteBase to `RewriteBase /myapp/`. If you have static files like CSS files and images to pass through, duplicate the line with the icons for each path you want to allow.
View
495 tutorial3.zh-cn.md
@@ -1,225 +1,272 @@
----
-layout: default
-title: web.py 0.3 新手指南
----
-
-# web.py 0.3 新手指南
-
-## 开始
-
-你知道Python同时你希望制作一个网站。 那么web.py正好提供了一种简单的方法。
-
-如果你希望读完整个指南, 你需要安装Python, web.py, flup, psycopg2, 和Postgres (或者等价的数据库和Python驱动)。 详细,可以查看 [webpy.org](http://webpy.org/).
-
-如果你已经有了一个web.py项目,请看看[升级](/docs/0.3/upgrade) 页面的相关信息。
-
-准备开始。
-
-## URL 处理
-
-任何网站最重要的部分就是它的URL结构。你的URL并不仅仅只是访问者所能看到并且能发给朋友的。它还规定了你网站运行的心智模型。在一些类似[del.icio.us](http://del.icio.us/)的流行网站 , URL甚至是UI的一部分。 web.py使这类强大的URL成为可能。
-
-在开始你的web.py程序之前,打开一个文本文件(文件名为code.py)输入:
-
- import web
-
-这条语句会导入web.py模块。
-
-现在我们需要把我们的URL结构告诉web.py。让我从下面这个简单的例子开始:
-
- urls = (
- '/', 'index'
- )
-
-第一部分是匹配URL的[正则表达式](http://osteele.com/tools/rework/),像`/``/help/faq``/item/(\d+)`等(`\d+`将匹配数字)。圆括号表示捕捉对应的数据以便后面使用。第二部分是接受请求的类名称,像`index``view``welcomes.hello` (`welcomes`模块的`hello`类),或者`get_\1``\1` 会被正则表达式捕捉到的内容替换,剩下来捕捉的的内容将被传递到你的函数中去。
-
-这行表示我们要URL`/`(首页)被一个叫`index`的类处理。
-
-现在我们需要创建一个列举这些url的application。
-
- app = web.application(urls, globals())
-
-这会告诉web.py去创建一个基于我们刚提交的URL列表的application。这个application会在这个文件的全局命名空间中查找对应类。
-
-## GET和POST: 区别
-
-现在我们需要来写`index`类。虽然大多数人只会看看,并不会注意你的浏览器在使用用于与万维网通信的HTTP语言。具体的细节并不重要,但是要理解web访问者请求web服务器去根据URL(像`/``/foo?f=1`)执行一个合适的函数(像`GET``POST`)的基本思想。
-
-`GET`是我们都熟悉的。它用于请求网页文本。当你在浏览器输入`harvard.edu`,它会直接访问Harvard的web服务器,去`GET /`。 第二个最有名的是`POST`,它经常被用在提交form,比如请求买什么东西。每当提交一个去做什么事情(像使用信用卡处理一笔交易)的请求时,你可以使用`POST`。这是关键,因为`GET`的URL可以被搜索引擎索引,并通过搜索引擎访问。虽然大部分页面你希望被索引,但是少数类似订单处理的页面你是不希望被索引的 (想象一下Google尝试去购买你网站上的所有东西)。
-
-在我们web.py的代码中,我们将这两个方法明确区分:
-
- class index:
- def GET(self):
- return "Hello, world!"
-
-当有人用`GET`请求`/`时,这个`GET`函数随时会被web.py调用。
-
-好了,限制我们只需要最后一句就写完了。这行会告诉web.py开始提供web页面:
-
- if __name__ == "__main__": app.run()
-
-这会告诉web.py为我们启动上面我们写的应用。
-
-现在注意,即使我已经在这里说了很多,但我们真正有5行这些代码。这就是你需要编写的一个完整的web.py应用。如果你在命令行下面,请输入:
-
- $ python code.py
- http://0.0.0.0:8080/
-
-现在你的web.py应用正运行在你电脑上的一个真正的web服务器上。 访问那个URL,然后你应该看到"Hello, world!" (你可以通过把IP地址/端口加在"code.py"的后面,来控制web.py在哪里启动服务器。你也可以让它运行在`fastcgi``scgi`服务器上)。
-
-**注意:** 如果你不能或者不想使用默认端口,你可以使用这样的命令来指定端口号:
-
- $ python code.py 1234
-
-## 模板
-
-在 Python 中写 HTML 不是聪明的选择,相反在 HTML 中写 Python 则有趣的多。幸运的是,`web.py` 让这件事情做得简单而又漂亮。
-
-**注意:** 老版本的 `web.py` 使用 [Cheetah 模板系统](http://www.cheetahtemplate.org/),你可以也欢迎使用其他模板系统,但它可能不会被长久支持。
-
-给模板新建一个目录(命名为 `templates`),在该目录下新建一个以 `.html` 结尾的文件,内容如下:
-
- <em>Hello</em>, world!
-
-你也可以在模板中使用 `web.py` 模板支持代码:
-
- $def with (name)
-
- $if name:
- I just wanted to say <em>hello</em> to $name.
- $else:
- <em>Hello</em>, world!
-
-如上,该模板看起来就像 python 文件一样,除了顶部的 `def with` (表示从模板将从这后面取值)和总是位于代码段之前的`$`。当前,`template.py` 首先请求模板文件的首行 `$def` 。当然,你要注意 `web.py` 将会转义任何任何用到的变量,所以当你将 `name` 的值设为是一段 HTML 时,它会被转义显示成纯文本。如果要关闭该选项,可以写成 `$:name` 来代替 `$name`
-
-回看再看 `code.py`。在第一行之下添加:
-
- render = web.template.render('templates/')
-
-这会告诉web.py到你的模板目录中去查找模板。然后把 `index.GET`改成:
-告诉 `web.py` 在你的模板目录下查找模板文件。修改 `index.GET`
-
- name = 'Bob'
- return render.index(name)
-
-('index' 是模板的名字,'name' 是传入模板的一个参数)
-
-访问站点它将显示 hello Bob。
-
-但是如果我们想让用户自行输入他的名字,么办?如下:
-
- i = web.input(name=None)
- return render.index(i.name)
-
-访问 `/` 将显示 hello world,访问 `/?name=Joe` 将显示 hello Joe。
-
-URL 的后面的 `?` 看起来不好看?修改下 URL 配置:
-
- '/(.*)', 'index'
-
-然后修改下 `index.GET`:
-
- def GET(self, name):
- return render.index(name)
-
-现在访问 `/Joe` 看看,它会显示 hello Joe。
-
-如果学习更多关于 web.py 的模板处理,请访问 [web.py 模板](/docs/0.3/templetor).
-
-## 数据库操作
-
-**注意:** 在你开始使用数据库之前,确保你已经安装了合适的数据库访问库。比如对于MySQL数据库,使用 [MySQLdb](http://sourceforge.net/project/showfiles.php?group_id=22307) ,对于Postgres数据库使用[psycopg2](http://initd.org/pub/software/psycopg/)。
-
-首先你需要创建一个数据库对象。
-
- db = web.database(dbn='postgres', user='username', pw='password', db='dbname')
-
-(根据需要修改这里 -- 尤其是`username``password``dbname` -- 。 MySQL用户还需要把 `dbn` 定义改为 `mysql`。)
-
-这就是所有你需要做的 -- web.py将会自动处理与数据库的连接和断开。
-
-使用的的数据库引擎管理工具,在你的库中创建一个简单的表:
-
- CREATE TABLE todo (
- id serial primary key,
- title text,
- created timestamp default now(),
- done boolean default 'f' );
-
-然后初始化行:
-
- INSERT INTO todo (title) VALUES ('Learn web.py');
-
-我们回来继续编辑 `code.py` ,把 `index.GET` 改成下面的样子,替换整个函数:
-
- def GET(self):
- todos = db.select('todo')
- return render.index(todos)
-
-然后把URL列表改回来,只保留 `/`:
-
- '/', 'index',
-
-像这样编辑并替换 `index.html` 的全部内容:
-
- $def with (todos)
- <ul>
- $for todo in todos:
- <li id="t$todo.id">$todo.title</li>
- </ul>
-
-再访问你的网站,然后你可以看到你的todo item: "Learn web.py"。恭喜你!你已经完整地写好了一个可以从数据库读取数据的程序。现在让我们同样再写一个可以把数据写入数据库的程序。
-
-在 `index.html`尾部添加:
-
- <form method="post" action="add">
- <p><input type="text" name="title" /> <input type="submit" value="Add" /></p>
- </form>
-
-然后把你的URL列表改为:
-
- '/', 'index',
- '/add', 'add'
-
-(你必须要非常小心那些逗号。如果你省略他们,Python会把所有字符串连接起来,变成 `'/index/addadd'`)
-
-现在添加另一个类:
-
- class add:
- def POST(self):
- i = web.input()
- n = db.insert('todo', title=i.title)
- raise web.seeother('/')
-
-(注意现在我们正在使用 `POST`)
-
-`web.input` 可以让你访问用户通过form提交的任何数据。
-
-注意: 如果要访问多个相同名字的字段,请使用list的格式(比如:一串name="name"的多选框):
-
- post_data=web.input(name=[])
-
-`db.insert` 把数据插入数据表 `todo` ,然后把新的行号返回给你。 `seeother` 把用户重定向到指定的URL。
-
-一些快速补充说明: `db.update``db.insert` 差不多,除了它返回的行号是直接从sql语句里面提取的(`WHERE ID=2`)。
-
-`web.input``db.query`已经其他web.py中的函数返回"Storage objects",这些东西就像字典,你除了可以 `d['foo']`之外,你还可以 `d.foo`。这可以让代码更加干净。
-
-你可以在[the documentation](/docs/0.3)找到这方面具体的细节以及所有web.py的函数说明。
-
-## 开发
-
-web.py 还有一些帮助我们debug的工具。当它在内建的服务器中运行时,它会一debug模式启动程序。在debug模式中,任何代码、模板的修改,都会让服务器重新加载它们,然后还会输出有用的错误消息。
-
-只有在生产环境中debug模式是关闭的。如果你想禁用debug模式,你可以在创建程序/模板前添加像这样的行。
-
- web.config.debug = False
-
-我们的指南就到这里了。如果要做更多很酷的东西,你可以先查看一下文档。
-
-## 下一步是什么?
-
-* [更多文档](/docs/0.3)
-* [Cookbook](/cookbook)
+---
+layout: default
+title: web.py 0.3 新手指南
+---
+
+# web.py 0.3 新手指南
+
+* [开始](#starting)
+* [URL处理](#urlhandling)
+* [GET和POST的区别](#getpost)
+* [启动服务](#start)
+* [模板](#templating)
+* [表单](#forms)
+* [数据库](#databasing)
+* [开发](#developing)
+* [下一步做什么?](#whatnext)
+
+## 开始
+
+你知道Python同时你希望制作一个网站。 那么web.py正好提供了一种简单的方法。
+
+如果你希望读完整个指南, 你需要安装Python, web.py, flup, psycopg2, 和Postgres (或者等价的数据库和Python驱动)。 详细,可以查看 [webpy.org](http://webpy.org/).
+
+如果你已经有了一个web.py项目,请看看[升级](/docs/0.3/upgrade) 页面的相关信息。
+
+准备开始。
+
+<a name="urlhandling"> </a>
+## URL 处理
+
+任何网站最重要的部分就是它的URL结构。你的URL并不仅仅只是访问者所能看到并且能发给朋友的。它还规定了你网站运行的心智模型。在一些类似[del.icio.us](http://del.icio.us/)的流行网站 , URL甚至是UI的一部分。 web.py使这类强大的URL成为可能。
+
+在开始你的web.py程序之前,打开一个文本文件(文件名为code.py)输入:
+
+ import web
+
+这条语句会导入web.py模块。
+
+现在我们需要把我们的URL结构告诉web.py。让我从下面这个简单的例子开始:
+
+ urls = (
+ '/', 'index'
+ )
+
+第一部分是匹配URL的[正则表达式](http://osteele.com/tools/rework/),像`/``/help/faq``/item/(\d+)`等(`\d+`将匹配数字)。圆括号表示捕捉对应的数据以便后面使用。第二部分是接受请求的类名称,像`index``view``welcomes.hello` (`welcomes`模块的`hello`类),或者`get_\1``\1` 会被正则表达式捕捉到的内容替换,剩下来捕捉的的内容将被传递到你的函数中去。
+
+这行表示我们要URL`/`(首页)被一个叫`index`的类处理。
+
+现在我们需要创建一个列举这些url的application。
+
+ app = web.application(urls, globals())
+
+这会告诉web.py去创建一个基于我们刚提交的URL列表的application。这个application会在这个文件的全局命名空间中查找对应类。
+
+<a name="getpost"> </a>
+## GET和POST: 区别
+
+现在我们需要来写`index`类。虽然大多数人只会看看,并不会注意你的浏览器在使用用于与万维网通信的HTTP语言。具体的细节并不重要,但是要理解web访问者请求web服务器去根据URL(像`/``/foo?f=1`)执行一个合适的函数(像`GET``POST`)的基本思想。
+
+`GET`是我们都熟悉的。它用于请求网页文本。当你在浏览器输入`harvard.edu`,它会直接访问Harvard的web服务器,去`GET /`。 第二个最有名的是`POST`,它经常被用在提交form,比如请求买什么东西。每当提交一个去做什么事情(像使用信用卡处理一笔交易)的请求时,你可以使用`POST`。这是关键,因为`GET`的URL可以被搜索引擎索引,并通过搜索引擎访问。虽然大部分页面你希望被索引,但是少数类似订单处理的页面你是不希望被索引的 (想象一下Google尝试去购买你网站上的所有东西)。
+
+在我们web.py的代码中,我们将这两个方法明确区分:
+
+ class index:
+ def GET(self):
+ return "Hello, world!"
+
+当有人用`GET`请求`/`时,这个`GET`函数随时会被web.py调用。
+
+好了,限制我们只需要最后一句就写完了。这行会告诉web.py开始提供web页面:
+
+ if __name__ == "__main__": app.run()
+
+这会告诉web.py为我们启动上面我们写的应用。
+
+
+现在注意,即使我已经在这里说了很多,但我们真正有5行这些代码。这就是你需要编写的一个完整的web.py应用。
+为了更方便的使用,你的完整代码应该像下面这样:
+
+ import web
+
+ urls = (
+ '/', 'index'
+ )
+
+ class index:
+ def GET(self):
+ return "Hello, world!"
+
+ if __name__ == "__main__":
+ app = web.application(urls, globals())
+ app.run()
+
+
+
+<a name="start"> </a>
+## Start the server
+
+ 如果你在命令行下面,请输入::
+ $ python code.py
+ http://0.0.0.0:8080/
+
+现在你的web.py应用正运行在你电脑上的一个真正的web服务器上。 访问那个URL,然后你应该看到"Hello, world!" (你可以通过把IP地址/端口加在"code.py"的后面,来控制web.py在哪里启动服务器。你也可以让它运行在`fastcgi``scgi`服务器上)。
+
+**注意:** 如果你不能或者不想使用默认端口,你可以使用这样的命令来指定端口号:
+
+ $ python code.py 1234
+
+
+
+<a name="templating"> </a>
+## 模板
+
+在 Python 中写 HTML 不是聪明的选择,相反在 HTML 中写 Python 则有趣的多。幸运的是,`web.py` 让这件事情做得简单而又漂亮。
+
+**注意:** 老版本的 `web.py` 使用 [Cheetah 模板系统](http://www.cheetahtemplate.org/),你可以也欢迎使用其他模板系统,但它可能不会被长久支持。
+
+给模板新建一个目录(命名为 `templates`),在该目录下新建一个以 `.html` 结尾的文件,内容如下:
+
+ <em>Hello</em>, world!
+
+你也可以在模板中使用 `web.py` 模板支持代码:
+
+ $def with (name)
+
+ $if name:
+ I just wanted to say <em>hello</em> to $name.
+ $else:
+ <em>Hello</em>, world!
+
+如上,该模板看起来就像 python 文件一样,除了顶部的 `def with` (表示从模板将从这后面取值)和总是位于代码段之前的`$`。当前,`template.py` 首先请求模板文件的首行 `$def` 。当然,你要注意 `web.py` 将会转义任何任何用到的变量,所以当你将 `name` 的值设为是一段 HTML 时,它会被转义显示成纯文本。如果要关闭该选项,可以写成 `$:name` 来代替 `$name`
+
+回看再看 `code.py`。在第一行之下添加:
+
+ render = web.template.render('templates/')
+
+这会告诉web.py到你的模板目录中去查找模板。然后把 `index.GET`改成:
+告诉 `web.py` 在你的模板目录下查找模板文件。修改 `index.GET`
+
+ name = 'Bob'
+ return render.index(name)
+
+('index' 是模板的名字,'name' 是传入模板的一个参数)
+
+访问站点它将显示 hello Bob。
+
+但是如果我们想让用户自行输入他的名字,么办?如下:
+
+ i = web.input(name=None)
+ return render.index(i.name)
+
+访问 `/` 将显示 hello world,访问 `/?name=Joe` 将显示 hello Joe。
+
+URL 的后面的 `?` 看起来不好看?修改下 URL 配置:
+
+ '/(.*)', 'index'
+
+然后修改下 `index.GET`:
+
+ def GET(self, name):
+ return render.index(name)
+
+现在访问 `/Joe` 看看,它会显示 hello Joe。
+
+如果学习更多关于 web.py 的模板处理,请访问 [web.py 模板](/docs/0.3/templetor).
+
+<a name="forms"> </a>
+## 表单
+
+web.py的form模块能够帮助你生成HTML表单;获取用户的输入,并在处理或添加到数据库之前对其进行内容的验证。
+如果你要学习更多关于form模块的使用,请查看[帮助文档](/docs/0.3)或者[Form](/form)类库的链接
+
+
+<a name="databasing"> </a>
+## 数据库操作
+
+**注意:** 在你开始使用数据库之前,确保你已经安装了合适的数据库访问库。比如对于MySQL数据库,使用 [MySQLdb](http://sourceforge.net/project/showfiles.php?group_id=22307) ,对于Postgres数据库使用[psycopg2](http://initd.org/pub/software/psycopg/)。
+
+首先你需要创建一个数据库对象。
+
+ db = web.database(dbn='postgres', user='username', pw='password', db='dbname')
+
+(根据需要修改这里 -- 尤其是`username``password``dbname` -- 。 MySQL用户还需要把 `dbn` 定义改为 `mysql`。)
+
+这就是所有你需要做的 -- web.py将会自动处理与数据库的连接和断开。
+
+使用的的数据库引擎管理工具,在你的库中创建一个简单的表:
+
+ CREATE TABLE todo (
+ id serial primary key,
+ title text,
+ created timestamp default now(),
+ done boolean default 'f' );
+
+然后初始化行:
+
+ INSERT INTO todo (title) VALUES ('Learn web.py');
+
+我们回来继续编辑 `code.py` ,把 `index.GET` 改成下面的样子,替换整个函数:
+
+ def GET(self):
+ todos = db.select('todo')
+ return render.index(todos)
+
+然后把URL列表改回来,只保留 `/`:
+
+ '/', 'index',
+
+像这样编辑并替换 `index.html` 的全部内容:
+
+ $def with (todos)
+ <ul>
+ $for todo in todos:
+ <li id="t$todo.id">$todo.title</li>
+ </ul>
+
+再访问你的网站,然后你可以看到你的todo item: "Learn web.py"。恭喜你!你已经完整地写好了一个可以从数据库读取数据的程序。现在让我们同样再写一个可以把数据写入数据库的程序。
+
+在 `index.html`尾部添加:
+
+ <form method="post" action="add">
+ <p><input type="text" name="title" /> <input type="submit" value="Add" /></p>
+ </form>
+
+然后把你的URL列表改为:
+
+ '/', 'index',
+ '/add', 'add'
+
+(你必须要非常小心那些逗号。如果你省略他们,Python会把所有字符串连接起来,变成 `'/index/addadd'`)
+
+现在添加另一个类:
+
+ class add:
+ def POST(self):
+ i = web.input()
+ n = db.insert('todo', title=i.title)
+ raise web.seeother('/')
+
+(注意现在我们正在使用 `POST`)
+
+`web.input` 可以让你访问用户通过form提交的任何数据。
+
+注意: 如果要访问多个相同名字的字段,请使用list的格式(比如:一串name="name"的多选框):
+
+ post_data=web.input(name=[])
+
+`db.insert` 把数据插入数据表 `todo` ,然后把新的行号返回给你。 `seeother` 把用户重定向到指定的URL。
+
+一些快速补充说明: `db.update``db.insert` 差不多,除了它返回的行号是直接从sql语句里面提取的(`WHERE ID=2`)。
+
+`web.input``db.query`已经其他web.py中的函数返回"Storage objects",这些东西就像字典,你除了可以 `d['foo']`之外,你还可以 `d.foo`。这可以让代码更加干净。
+
+你可以在[the documentation](/docs/0.3)找到这方面具体的细节以及所有web.py的函数说明。
+
+<a name="developing"> </a>
+## 开发
+
+web.py 还有一些帮助我们debug的工具。当它在内建的服务器中运行时,它会一debug模式启动程序。在debug模式中,任何代码、模板的修改,都会让服务器重新加载它们,然后还会输出有用的错误消息。
+
+只有在生产环境中debug模式是关闭的。如果你想禁用debug模式,你可以在创建程序/模板前添加像这样的行。
+
+ web.config.debug = False
+
+我们的指南就到这里了。如果要做更多很酷的东西,你可以先查看一下文档。
+
+<a name="whatnext"> </a>
+## 下一步是什么?
+
+* [更多文档](/docs/0.3)
+* [Cookbook](/cookbook)
* [code samples](/src)
Please sign in to comment.
Something went wrong with that request. Please try again.