Loading video player…

Templates and Setup

00:00 Creating Templates and Completing the Blog Site Setup. There are three template files that go with the two views. The first is a base template that contains common HTML elements.

00:13 The other two extend this base and correspond to views themselves. Create templates/base.html as follows.

00:48 This file is then extended by the templates used by the views. The listing() view uses a file called templates/listing.html that looks like this.

01:39 Notice how it uses the blogs query set sent in by the view. The query set is looped through, printing out the title attribute of each of the Blog objects found. Finally, to view an actual blog, create templates/view_blog.html.

02:28 The templates/listing.html and templates/view_blog.html files use the {% url %} tag to look up the URLs associated with the listing() and view_blog() views.

02:40 These URLs need to be registered in Blog/urls.py. Modify that file as follows.

03:19 The default urls.py file doesn’t import include(), so note that change to the second line. The rest of the lines import core/views.py and create the URL patterns for the listing() and view_blog() view functions.

03:36 So far, you have no way to create content. The Django admin provides a quick way to modify model objects. Modify core/admin.py to register the Blog object.

04:02 Everything is now in place. Use the following Django management commands to make and migrate the database models and run the server. The last command runs the Django development server. While it’s running, you can use a browser to visit the address shown onscreen to see the listing page, but without any data.

04:34 Adding Some Data. Since you haven’t created anything yet, the page will only show the title. Visit the Django admin at the address shown onscreen and log in using the superuser credentials you created earlier on.

04:51 Once inside, you can click the Add link next to the Blogs object to create some test data.

05:13 Alternatively, the sample code has a fixture containing example data. You can load the example data with the loaddata management command as seen here.

05:29 The fixture file contains two example blog entries. If you visit the site’s home page, then it should now show some content, as you see onscreen. Now that you have a working blog site with some data in it, next you’ll see how to detect whether users are logged in or not.

Become a Member to join the conversation.