Django: Beyond the SQL: Public pages
- Create your administration models
- Beyond the SQL
- Customization
- Add more data
- At this point, we have a standard set of database tables that we can use the same as we use any tables. We can look at them in Sequel Pro, and we can even display them using PHP on a server that doesn’t have Django installed. But that would be a waste of a very good framework.
- Public pages: Views
- In a web framework such as Django, you don’t have separate pages. What you have are views into a (usually) database. You’ll often have one template that supplies one view with as many pages as there are records in the database.
- Using the Django API
- The usefulness of Django is not that it can send flat files to web browsers. It is that Django provides an API for creating complex dynamic content that can be inserted into templates.
- Public pages: Templates
- http://docs.djangoproject.com/en/dev/ref/templates/builtins/
- Template inheritance
- Often, blogs will provide a means of linking to an individual post. That’s easy enough to do. We need to be a little careful of polluting our URL namespace, however. Posts can have any slug, but if we want to add new features to our web site, a post might end up interfering with those other URLs.
- Public pages: Linking
- If you want people to link to your posts, it’s a good idea to provide links. Django can provide the URL of a model’s instance using the {% url %} tag.
- If… Then
- If you take a look at post.html, there’s a potential problem with the author link. We link to the author’s home page, but homepage is an optional field. If the author doesn’t have a home page, that link will just go to the same page.
- Topic template
- Often, blogs will have a page showing all posts on a particular topic. Create a new template, “topic.html”:
- Include subtemplates
- We’ll also link the topics to their listing pages, but that’s going to make the topics “for” loop long. Rather than maintain it in two places, we’re going to separate it out to its own template file.
- Simple redirects
- If you take a look at our URL structure right now, we’ve got / for all postings, /postings/archive/slug for individual posts, and /postings/topic/slug for topical listings. There are three URLs that people might expect to use but that don’t have anything matching them: /postings, /postings/archive, and /postings/topic. The latter two I’ll leave to you as an exercise. It might make sense to limit the main page to only the top 20…