Work listing page
Add a page to list works.
In this section
Adding a view and template to list all works
How to link to an expression
Getting the latest expression for a work
Work listing view
On our app's homepage, we're going to list all the works.
Let's create the View code to handle this in views.py
. We'll use Django's generic ListView
view which does all the work for us.
Now create the matching template. Django will automatically look for a file called templates/reader/work_list.html
:
Finally, let's add this view legislation/urls.py
:
Now we need to run our server:
Visit http://localhost:8000 and see you list of by-laws!
We now have a basic listing page that shows the titles and FRBR URIs of all the works.
How can we create a link to read the content of a by-law? If we change the table to make the title a link, what exactly are we linking to?
Linking to an expression
Recall that a work may have multiple expressions. For example, there could be different languages and different amended versions of a work.
When we link to the content of a work, which expression are we linking to?
For our app, we want to link to the latest version of a work. We're also going to add a DEFAULT_LANGUAGE_CODE
setting for the site. We'll look for expressions that are in that language first, and then fall back to any language.
Let's add a helper method latest_expression
to the Work model to help us fetch the latest expression for the work.
Let's add the new DEFAULT_LANGUAGE_CODE
to legislation/settings.py
:
Now we can update the template to link to the latest expression.
We need to add the view and URL configuration for the expression
URL before these changes will work. We'll do that in the next section.
Last updated