Creating Webpages with Controllers in Django: Views and URL Configurations
- 5.1 Introduction
- 5.2 The Purpose of Views and URL Configurations
- 5.3 Step-by-Step Examination of Django's Use of Views and URL Configurations
- 5.4 Building Tag Detail Webpage
- 5.5 Generating 404 Errors for Invalid Queries
- 5.6 Shortening the Development Process with Django View Shortcuts
- 5.7 URL Configuration Internals: Adhering to App Encapsulation
- 5.8 Implementing the Views and URL Configurations to the Rest of the Site
- 5.9 Class-Based Views
- 5.10 Redirecting the Homepage
- 5.11 Putting It All Together
In Chapter 2: Hello World: Building a Basic Webpage in Django, we saw that the Controller is the only part of Django actually required to make a webpage (the relevant diagram is reprinted in Figure 5.1). However, we immediately ran into problems: we had no way to easily fetch and format data. Because the main function of websites revolves around data, the Controller is often described as the glue between Model and View despite the Controller’s independence.
Figure 5.1: Application of MVC Architecture Diagram
In this chapter, we return to the Controller, seen earlier in Chapter 2 and Chapter 4: Rapidly Producing Flexible HTML with Django Templates. We first re-examine how the two parts of the Controller, URL configurations and views, interact. We then use the cumulative knowledge we have gained to build dynamic webpages.
The Controller is central to Django and comes with a number of options. Once we have the basics, we look at how to handle problems that occur in views. We then look at ways to more rapidly code views (at the cost of developer control). Coding views enable us to very quickly build all the webpages for our site.
Before we finish the chapter, we also examine two special methods for creating Controllers, which become important later in the book.
This chapter assumes knowledge of HTTP and regular expressions. Primers on both are provided in Appendix A and Appendix E, respectively.