web.py wikihome of the web.py community web.py 0.2 tutorialStartingSo 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). For details, see webpy.org. If you have an existing web.py project, take a look at the upgrade page for info on migrating. Let's get started. URL HandlingThe 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, 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:
This imports the web.py module. Now we need to tell web.py our URL structure. Let's start out with something simple:
The first part is a regular expressions that matches a URL, like This line says we want the URL Now we need to write the
In our web.py code, we make the distinction between the two clear:
This Alright, now we just need to finish up with a final line telling web.py to start serving web pages:
This tells web.py to serve the URLs we listed above, looking up the classes in the global namespace of this file. 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. If you go to your command line and type:
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 Developingweb.py also has a few tools to help us with debugging. Before the 'if __name__' on last line, add:
This will give you more helpful error messages. And on the last line add
This tells web.py to use the web.reloader "middleware" (middleware is a wrapper function to add some functionality to your web server) which reloads your files whenever you edit them, so that you can see the changes in your web browser right away. (For some serious changes, though, you'll still have to restart the server.) You'll probably want to take this out when you make your site public, but it's great while developing. There's also TemplatingWriting 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. **Note: web.py currently also supports Cheetah templates. Read the former tutorial for more information. Let's make a new directory for our templates (we'll call it
Or you can use web.py's templating language to add code to your HTML:
Note: Currently, four spaces are required for indentation. As you can see, the templates look a lot like Python files except for the Now go back to
This tells web.py to look for templates in your templates directory. Then change
('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. Development tip: Add , Now change your URL line to:
and change the definition of
and delete the line setting name. Visit If you wish to learn more about web.py templates, vist the templetor page. DatabasingNote: Before you can start using a database, make sure you have the appropriate database library installed. For MySQL databases, use MySQLdb and for Postgre use psycopg2. Above your
(Adjust these -- especially Create a simple table in your database:
And an initial row:
Back in
and change back the URL handler to take just Edit
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
And change your URLs list to read:
(You've got to be very careful about those commas. If you omit them, Python adds the strings together and sees Now add another class:
(Notice how we're using
Quickly:
You can find the full details on these and all the web.py functions in the documentation. This ends the tutorial for now. Take a look at the documentation for lots more cool stuff you can do with web.py. wedding dress wedding gown bridal gown oil paintings oil painting prom dress prom gown prom dress |
Powered by infogami, which uses web.py. |