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:

  1. >>> from datetime import date
  2. >>> t = date.today()
  3. >>> t
  4. 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:

  1. >>> t.year
  2. 2020
  3. >>> date.strftime(t, '%b')
  4. 'March'
  5. >>> month = date.strftime(t, '%b')
  6. >>> year = t.year
  7. >>> title = "MyClub Event Calendar - %s %s" % (month,year)
  8. >>> title
  9. 'MyClub Event Calendar - March 2020'
  10. >>>

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):

  1. # \myclub_root\events\views.py
  2. 1 from django.shortcuts import render
  3. 2 from django.http import HttpResponse
  4. 3 from datetime import date
  5. 4
  6. 5
  7. 6 def index(request):
  8. 7 t = date.today()
  9. 8 month = date.strftime(t, '%b')
  10. 9 year = t.year
  11. 10 title = "MyClub Event Calendar - %s %s" % (month,year)
  12. 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’s datetime 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).

Your Second View: Dynamic Content - 图1

Figure 5-2: Your homepage with a dynamic title.