[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

code is separate components. Search the Web for more details. Similar techniques can
be used for separation of layout and login in the GUIs we studied earlier in this book,
but they also usually require larger frameworks or models to achieve.


Passing Parameters in Hardcoded URLs


Earlier, we passed parameters to CGI scripts by listing them at the end of a URL typed
into the browser’s address field—in the query string parameters part of the URL, after
the ?. But there’s nothing sacred about the browser’s address field. In particular, noth-
ing is stopping us from using the same URL syntax in hyperlinks that we hardcode or
generate in web page definitions.


For example, the web page from Example 15-14 defines three hyperlinks (the text be-
tween the and tags), which trigger our original tutor5.py script again (Exam-
ple 15-12), but with three different precoded sets of parameters.


Example 15-14. PP4E\Internet\Web\tutor5c.html


CGI 101

Common input devices: URL parameters


This demo invokes the tutor5.py server-side script again,
but hardcodes input data to the end of the script's URL,
within a simple hyperlink (instead of packaging up a form's
inputs). Click your browser's "show page source" button
to view the links associated with each list item below.

This is really more about CGI than Python, but notice that
Python's cgi module handles both this form of input (which is
also produced by GET form actions), as well as POST-ed forms;
they look the same to the Python CGI script. In other words,
cgi module users are independent of the method used to submit
data.

Also notice that URLs with appended input values like this
can be generated as part of the page output by another CGI script,
to direct a next user click to the right place and context; together
with type 'hidden' input fields, they provide one way to
save state between clicks.






1170 | Chapter 15: Server-Side Scripting

Free download pdf