Python-Ref > Dynamic web pages in Python > Passing arguments to dynamic webpages
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Passing arguments to dynamic webpages

How to generate a webpage based on some user-supplied data.
The following example shows how to query GET arguments from your python script. POST arguments are handled in the same way.
Using the publisher handler, you just have to give your function and keyword argument of the same name as the GET (or POST) argument you want to capture. This argument will then hold the value submitted to you page.
Expand/Shrink
Zdroj: (dpages4-1.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   
  4   page_template = '''
  5   <html>
  6   <head>
  7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8   <title>%s</title>
  9   </head>
 10   <body>
 11   %s
 12   </body>
 13   </html>'''
 14   
 15   def index( message="EMPTY"):
 16     title = "Echo - I will show whatever message you give me"
 17     body = "<div><b>The message is:</b> %s</div>" % message
 18     return page_template % (title, body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Echo - I will show whatever message you give me</title>
</head>
<body>
<div><b>The message is:</b> EMPTY</div>
</body>
</html>
Doba běhu: 8.4 ms
Expand/Shrink
URL:
dpages4-1.py?message=Hi,%20there
Zdroj: (dpages4-1.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   
  4   page_template = '''
  5   <html>
  6   <head>
  7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8   <title>%s</title>
  9   </head>
 10   <body>
 11   %s
 12   </body>
 13   </html>'''
 14   
 15   def index( message="EMPTY"):
 16     title = "Echo - I will show whatever message you give me"
 17     body = "<div><b>The message is:</b> %s</div>" % message
 18     return page_template % (title, body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Echo - I will show whatever message you give me</title>
</head>
<body>
<div><b>The message is:</b> Hi, there</div>
</body>
</html>
Doba běhu: 5.5 ms
The following example shows something more interesting and useful - it displays data from a database based on the data the user submitted.
Expand/Shrink
URL:
dpages4-2.py?name_start=M
Zdroj: (dpages4-2.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   
  4   page_template = '''
  5   <html>
  6   <head>
  7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8   <title>%s</title>
  9   </head>
 10   <body>
 11   %s
 12   </body>
 13   </html>'''
 14   
 15   def index( name_start=""):
 16     title = "Country list"
 17     body = "<h1>Countries whose name starts with '%s'</h1>" % name_start
 18     body += "<table>"
 19     connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost", cursorclass=DictCursor)
 20     cursor1 = connection.cursor()
 21   
 22     cursor1.execute( "SELECT Name,Population FROM Country WHERE Name LIKE '%s%%' ORDER BY Name;" % name_start)
 23     for i in range( cursor1.rowcount):
 24       data = cursor1.fetchone()
 25       body += "<tr><td>%s</td><td>%d</td></tr>" % (data["Name"], data["Population"])
 26     body += "</table>"
 27   
 28     cursor1.close()
 29     connection.close()
 30   
 31     return page_template % (title, body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Country list</title>
</head>
<body>
<h1>Countries whose name starts with 'M'</h1><table><tr><td>Macao</td><td>473000</td></tr><tr><td>Macedonia</td><td>2024000</td></tr><tr><td>Madagascar</td><td>15942000</td></tr><tr><td>Malawi</td><td>10925000</td></tr><tr><td>Malaysia</td><td>22244000</td></tr><tr><td>Maldives</td><td>286000</td></tr><tr><td>Mali</td><td>11234000</td></tr><tr><td>Malta</td><td>380200</td></tr><tr><td>Marshall Islands</td><td>64000</td></tr><tr><td>Martinique</td><td>395000</td></tr><tr><td>Mauritania</td><td>2670000</td></tr><tr><td>Mauritius</td><td>1158000</td></tr><tr><td>Mayotte</td><td>149000</td></tr><tr><td>Mexico</td><td>98881000</td></tr><tr><td>Micronesia, Federated States of</td><td>119000</td></tr><tr><td>Moldova</td><td>4380000</td></tr><tr><td>Monaco</td><td>34000</td></tr><tr><td>Mongolia</td><td>2662000</td></tr><tr><td>Montserrat</td><td>11000</td></tr><tr><td>Morocco</td><td>28351000</td></tr><tr><td>Mozambique</td><td>19680000</td></tr><tr><td>Myanmar</td><td>45611000</td></tr></table>
</body>
</html>
Doba běhu: 337.1 ms
Expand/Shrink
URL:
dpages4-2.py?name_start=Au
Zdroj: (dpages4-2.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   
  4   page_template = '''
  5   <html>
  6   <head>
  7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8   <title>%s</title>
  9   </head>
 10   <body>
 11   %s
 12   </body>
 13   </html>'''
 14   
 15   def index( name_start=""):
 16     title = "Country list"
 17     body = "<h1>Countries whose name starts with '%s'</h1>" % name_start
 18     body += "<table>"
 19     connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost", cursorclass=DictCursor)
 20     cursor1 = connection.cursor()
 21   
 22     cursor1.execute( "SELECT Name,Population FROM Country WHERE Name LIKE '%s%%' ORDER BY Name;" % name_start)
 23     for i in range( cursor1.rowcount):
 24       data = cursor1.fetchone()
 25       body += "<tr><td>%s</td><td>%d</td></tr>" % (data["Name"], data["Population"])
 26     body += "</table>"
 27   
 28     cursor1.close()
 29     connection.close()
 30   
 31     return page_template % (title, body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Country list</title>
</head>
<body>
<h1>Countries whose name starts with 'Au'</h1><table><tr><td>Australia</td><td>18886000</td></tr><tr><td>Austria</td><td>8091800</td></tr></table>
</body>
</html>
Doba běhu: 332.6 ms