Your Second View: Dynamic Content
Our simple index view does an excellent job of demonstrating how content from a Django view is rendered to the browser, but it’s not a very good example of a modern web page. That’s because it’s static, i.e., the page content is always the same.
For a modern interactive site, you want the content of your pages to change dynamically based on interaction with the user and program logic.
Static Pages Still Have a Place
Not all pages need to be dynamic. For example, about pages, terms and conditions, and privacy statements don’t change regularly and are often saved as static HTML pages.
Django’s developers thought of this and made managing static pages from within your Django administration dead easy with the built-in flatpages
app. We will have a look at the flatpages
app in Chapter 18.
With our event calendar, it would be good for the page heading to include the month and year. This can’t be hard-coded into the HTML, so we need to develop a way to show the current month and year in the title.
We should probably add an actual calendar too—given it will not be much of an event calendar without you being able to see the calendar!
Before we modify our index
view, let’s use the interactive shell to test out some new code. You can use either the Python interactive shell or Django’s shell; the code will work in both:
>>> from datetime import date
>>> t = date.today()
>>> t
datetime.date(2020, 3, 22)
To start, we’re importing the date
class from Python’s datetime
module. We then assign today’s date to the variable t
. Now we have some useful information we can use to extract the current month and year:
>>> t.year
2020
>>> date.strftime(t, '%b')
'March'
>>> month = date.strftime(t, '%b')
>>> year = t.year
>>> title = "MyClub Event Calendar - %s %s" % (month,year)
>>> title
'MyClub Event Calendar - March 2020'
>>>
The first line should be straightforward—we’re accessing the year attribute of the datetime
object t
.
The next bit of code is more interesting. We’re using the strftime
(string format) module from Python to format t
. The %b
format string converts datetime
object t
into a string containing the month formatted to its abbreviated (3-character) name. strftime
is a handy module for formatting dates and times. If you want to learn more check out the Python strftime
documentation.
In the last bit of test code, we’re putting the month and year into two variables and then using Python’s string formatting syntax to generate a dynamic title for our event calendar page.
Now we’ve got some working code, let’s jump over to our index
view and put it to practical use (changes in bold):
# \myclub_root\events\views.py
1 from django.shortcuts import render
2 from django.http import HttpResponse
3 from datetime import date
4
5
6 def index(request):
7 t = date.today()
8 month = date.strftime(t, '%b')
9 year = t.year
10 title = "MyClub Event Calendar - %s %s" % (month,year)
11 return HttpResponse("<h1>%s</h1>" % title)
Let’s have a look at the changes to our index
view:
- Line 3. Import the
date
class from Python’sdatetime
module. - Line 7. Assign today’s date to the variable
t
. - Line 8. Set the
month
variable to the 3-character month. - Line 9. Assign the current year to the variable
year
. - Line 10. Use a Python string format function to create the dynamic title for our page.
- Line 11. Use another Python string format to return our dynamic title as HTML.
If you run the development server, you should see the homepage now shows the event calendar title with the current date appended (Figure 5-2).
Figure 5-2: Your homepage with a dynamic title.