所需积分/C币:9 2018-08-09 17:53:35 246KB PDF

web.py ,Python Web开发框架官方文档 API,以及代码示例程序
Contents 1 Accessing User Input 1.1 GET 1.2 POST 1.3 Multiple inputs with same name 4 1. 4 File upload 2 Accessing the database 2.1 Create database object 2.1.1 Multiple databases 2.2 Operations Insertin 2 Selecting 2.2.3 Updating 2.2.4 Deleting 2.2.5 Multiple Inserts 2.2.6 Advanced querying 2. 2.7 Transactions 3 Templating 666677999 Variable substitution ··· 3.2 Newline suppression 3.3 Expressions 10 3.4 Comments ...,,,10 3.5 Code 10 3.6 Python integration 3. 7 Turning Off Filter 12 3.8 Including/nesting templates .12 3.9E 13 4 Deploying web. py applications 15 4.1 FastCGI 15 4.1.1Configuringlighttpd 15 4.2 nginx unicorn 16 5 web pv API 17 web. application·..·:,· 17 5.2 20 5.3 ....23 web form 25 b.http 27 5.6 web session 7 5.7 web template 8 5.8 web, utils 32 web. webapi·· 42 6 Getting Started 7 Indices and table 49 Python Module Index 51 web. py Documentation, Release 0.37 Contents Contents web. py Documentation, Release 0.37 Contents CHAPTER Accessing User Input While building web applications, one basic and important thing is to respond to the user input that is sent to the server Web. py makes it easy to access that whether it is parameters in the url(GET request)or the form data(POst or PUT request). The web input( method returns a dictionary -like object(more specifically a web storage object) that contains the user input, whatever the request method To access the URL parameters(? key=value) from the web input object, just use web input(). key. 1.1 GET For a URL which looks like /page?id=l&action=edit, you do class Page(object) def GeT(self) data- web inpu=() id= int(data. id) all the inputs are now strings. Cast it to int, to get integer ction data. actior Key Error exception is thrown if key is not there in the URL parameters. Web. py makes it easier to handle that with default values to web input class Page(object) def GET(self) data= web inpu=(id=l, action=read id, action ins(data. id), cata. action 1.2 POST It works exactly the same way with POst method. If you have a form with name and password elements, you would do class Login (object): def post (self) dala b.⊥npu-() name, password- data name, data. password 3 web. py documentation Release 0.37 1.3 Multiple inputs with same name What if you have a URL which looks like /page?id=l&id=2&id=3 or you have a form with multiple selects? What would weh input(). id give us? It simply swallows all but one value. But to let web input know that we're ex pecting more values with the same name is simple. Just pass / as the default argument for that name class Page(object) def Get(self) data web. inpu=(id=[]) ids data id now,ids is a list with all the ids 1.4 File uploads Uploading files is easy with web. py. web. input() takes care of that too. Just make sure that the upload form has an attribute enctype=multipart/form-data. The input() gives you filename and value, which are the uploaded file name and the contents of it, respectively. To make things simpler, it also gives you file, a file-like object if you pass myfile=l) where myfile is the name of the input element in your form class Upload(object): de£CET(sc1f) return render upload() def post(self) data= web. input (myfile=i H fp= data. myfile save(fp) f fp filename, fp. read() gives name and contents of the fi7 lass Upload(object) def post(self) data= web input()# notice that 'myfile=f) is missing here f data, myfile cave ( fp filename, fp value) Chapter 1. Accessing User Input CHAPTER 2 Accessing the database Web. py provides a simple and uniform interface to the database that you want to work with, whether it is PostgresQl MySQL, SQLite or any other. It doesnt try to build layers between you and your database. Rather, it tries to make it easy to perform common tasks, and get out of your way when you need to do more advanced things 2.1 Create database object le irsu thing to work with databases from web. py is to create a create a database object with web database( It returns database object, which has convenient methods for you to use Make sure that you have appropriate database library installed (psycopg2 for PostgreSQL, MysQldb for MysQL, sylite3 for SQLite) db- wer database(abn-'postgres, db='dbname'r user-'username', pw='password) dbn for My sQl is mysql and sqlite for SQlite SQLite doesnt take user pw parameters 2.1.1 Multiple databases Working with more databases is not at all difficult with web. py. Here's what you do dbl= web database(dbn-lpostgres', ch-'dbnamcl'r user=lusernamel', pv='password2') db2- web database(dbn-lpostgres', ch-'dbname2', user='username2', pw='password2 And use db1, db2 to access those databases respectively 2.2 Operations web database returns an object which provide you all the functionality to insert, select, update and delete data from your database. For each of the methods on db below, you can pass_test=True to see the sQl statement rather than executing it 2.2.1 Inserting Insert an entry into table ' user userid= ab insert('user', firstname="Bob", lastname="Smith", joindate=web. SQLLiteral("NCw()")) web. py documentation Release 0.37 The first argument is the table name and the rest of them are set of named arguments which represent the fields in the table. If values are not given, the database may create default values or issue a warning For bulk insertion rather than inserting record by record, use Multiple inserts rather 2.2.2 Selecting The select method is used for selecting rows from the database. It returns a web. iterbetter object, which can be looped through To select all the rows from the user table, you would simply do users ab select (user) For the real world use cases, select method takes vars, what, where, order, group, limit, offset, and _test optional parameters users ab select(' users, where=1c>100") To prevent SQL injection attacks, you can use Skey in where clause and pass the vars which has(key: value j vars dial (name="Bob") results =ab select(users', where="name Sname", vars=vars,test=True) >> results <SCI:SELECT FROM users WHERE name =Bob 2.2.3 Updating The update method accepts same kind of arguments as Select. It returns the number of rows updated num_updated- db, update(users', where=id =10", firstname ="Foo) 2.2.4 Deleting delete method returns the number of rows deleted. It also accepts using an parameters Selecting for more details on vars num deleted- db. delete(users, where-"=10 2.2.5 Multiple Inserts The multiple_insert method on the db object allows you to do that. all thats needed is to prepare a list of dictionaries one for each row to be inserted, each with the same set of keys and pass it to multiple_insert along with the table name. It returns the list of ids of the inserted rows The value of db supports_multiple_insert tells you if your database supports multiple inserts values=[t"name":fo0","email":"foodexample.com"h,t"name":"bar","email":"bardexdmple.com";3 db. multipleinsert(' person, values-values) Chapter 2. Accessing the database


关注 私信 TA的资源