Chapter 21 · 10 min read
Code Reading
Code Reading
Why practise reading code
Most of the time a developer spends with Django is spent reading code: your own from last week, a colleague's, or an example in the documentation. This section gives you ten short snippets. Each one is followed by four questions that ask you to predict what the code does, without running it.
How to read a snippet
- Find the entry point. For web code, that is usually a URL pattern, then the view it points to.
- Follow the data. Ask what goes in, what the code does to it, and what comes out.
- Check the names. Names such as
blog:home,post_listandregistration/login.htmlfollow conventions that tell you where things are. - Look for the unusual. A
commit=False, areverse_lazyor aUserPassesTestMixinusually exists for a reason. Ask what would go wrong without it. - Predict before you answer. Decide what you expect to happen, then pick the option.
Skills you will practise
| Snippet | Topic |
|---|---|
| 1 | URL patterns, converters and reverse() |
| 2 | A function view and its template |
| 3 | The template language |
| 4 | Model fields and relations |
| 5 | ORM behaviour in the shell |
| 6 | Admin configuration |
| 7 | A form view with POST handling |
| 8 | A class-based view with access rules |
| 9 | Automated tests |
| 10 | Production settings |
Each snippet is a separate chapter, and each has its own practice questions.
