Page 268 -
P. 268
no dumb questions
Q: Where are the HTML templates used in the include_ Q: And you did this because you are using MVC?
header() and include_footer() functions?
A: Partly, yes. The reason the MVC pattern is being followed is to
A: They are included with the yate module’s download. Go ensure that the model code is separate from the view code, which
ahead and grab them from the Head First Python support website, are both separate from the controller code. No matter the size of the
and put them into a folder of your choice. project, following MVC can make your life easier.
Q: Why do I need yate at all? Why not include the HTML that I Q: But surely MVC is overkill for something this small?
need right in the code and generate it with print() as needed?
A: We don’t think so, because you can bet that your webapp will
A: You could, but it’s not as flexible as the approach shown grow, and when you need to add more features, the MVC “separation
here. And (speaking from bitter experience) using a collection of of duties” really shines.
print() statements to generate HTML works, but it turns your
code into an unholy mess.
Let’s get to know the yate module even more. With the code downloaded and tucked away in an easy-to-
find folder, load the module into IDLE and press F5 to take it for a spin. Let’s start by testing the start_
response() function. The CGI standard states that every web response must start with a header line that
indictes the type of the data included in the request, which start_response() lets you control:
>>> start_response() The default CGI response header,
'Content-type: text/html\n\n' plus variations on a theme.
>>> start_response("text/plain")
'Content-type: text/plain\n\n'
>>> start_response("application/json")
'Content-type: application/json\n\n'
The include_header() function generates the start of a web page and let’s you customizee its title:
>>> include_header("Welcome to my home on the web!")
'<html>\n<head>\n<title>Welcome to my home on the web!</title>\n<link type="text/css"
rel="stylesheet" href="/coach.css" />\n</head>\n<body>\n<h1>Welcome to my home on the web!</
h1>\n'
This all looks a little bit messy, but don’t worry; it’s meant
to be processed by your web browser, NOT by you. Your web
browser will have no difficulty working with this HTML. Note
the inclusion of a link to a CSS file (more on this in a bit).
232 Chapter 7