Our Hello World view was instructive in demonstrating the basics of how Django works, but it wasn't an example of a dynamic web page, because the content of the page is always the same. Every time you view /hello/
, you'll see the same thing; it might as well be a static HTML file.
For our second view, let's create something more dynamic-a web page that displays the current date and time. This is a nice, simple next step, because it doesn't involve a database or any user input-just the output of your server's internal clock. It's only marginally more exciting than Hello World, but it'll demonstrate a few new concepts. This view needs to do two things: calculate the current date and time, and return an HttpResponse
containing that value. If you have experience with Python, you know that Python includes a datetime
module for calculating dates. Here's how to use it:
>>> import datetime >>> now = datetime.datetime.now() >>>...