Complete Code for the Django Tutorial, Step by Step
Are you stuck in the Django tutorial? Compare your code to this complete and tested solution of the Django tutorial on GitHub. I have worked through the tutorial and made a commit after each change, so you can not only look at the final outcome but also at each individual step along the way!
The official Django tutorial, where you build a simple poll application, is a great starting point for learning Django. The tutorial is well written and explains each step of building the polls app in detail.
But if you misunderstand an instruction or make a typo, you can end up with an error that might be incomprehensible to you. As each step in the tutorial builds on the previous step, it might be hard to tell where you made a mistake and what your code should actually look like.
For example, if you are stuck in the section of Part 3 where you remove the hardcoded URLs in templates, you can jump to the corresponding tag and see what the template should like.
Step-by-step
Here is the complete list of all steps in the tutorial. Follow the link to explore the code directly on GitHub. Alternatively, you can download each step as a Zip archive.
Part 1:
- Part 1: Creating a project [ZIP]
- Part 1: Creating the Polls app [ZIP]
- Part 1: Writing your first view [ZIP]
Part 2:
- Part 2: Database setup [ZIP]
- Part 2: Creating models [ZIP]
- Part 2: Activating models [ZIP]
- Part 2: Playing with the API [ZIP]
- Part 2: Introducing the Django Admin [ZIP]
Part 3:
- Part 3: Writing more views [ZIP]
- Part 3: Write views that actually do something [ZIP]
- Part 3: Write views that actually do something – with templates [ZIP]
- Part 3: Write views that actually do something – A shortcut: render() [ZIP]
- Part 3: Raising a 404 error [ZIP]
- Part 3: Raising a 404 error – A shortcut: get_object_or_404() [ZIP]
- Part 3: Use the template system [ZIP]
- Part 3: Removing hardcoded URLs in templates [ZIP]
- Part 3: Namespacing URL names [ZIP]
Part 4:
- Part 4: Write a simple form [ZIP]
- Part 4: Write a simple form – results view [ZIP]
- Part 4: Use generic views: Less code is better [ZIP]
Part 5:
- Part 5: Writing our first test – Create a test to expose the bug [ZIP]
- Part 5: Writing our first test – Fixing the bug [ZIP]
- Part 5: Writing our first test – More comprehensive tests [ZIP]
- Part 5: Test a view – Improving our view [ZIP]
- Part 5: Test a view – Testing our new view [ZIP]
- Part 5: Test a view – Testing the DetailView [ZIP]
Part 6:
Part 7:
- Part 7: Customize the admin form – reordering the fields [ZIP]
- Part 7: Customize the admin form – split the form [ZIP]
- Part 7: Adding related objects – register Choice with the admin [ZIP]
- Part 7: Adding related objects – remove Choice from admin, add inline [ZIP]
- Part 7: Adding related objects – use TabularInline (instead of StackedInline) [ZIP]
- Part 7: Customize the admin change list – use list_display option [ZIP]
- Part 7: Customize the admin change list – include was_published_recently() [ZIP]
- Part 7: Customize the admin change list – add attributes to was_published_recently() [ZIP]
- Part 7: Customize the admin change list – add list_filter [ZIP]
- Part 7: Customize the admin change list – add search_fields [ZIP]
- Part 7: Customize the admin look and feel – Customizing your project’s templates [ZIP]
I hope this will save some people some head scratching while completing the Django Tutorial. Let me know if you find this useful!
What’s next?
Once you have completed the Django tutorial, you might wonder how to move on. Here is an article outlining possible next steps after the Django tutorial.
Near the end of Part 5, they suggest writing more tests, like for the ResultsView, but I can’t figure out what the get_queryset() should look like for that (since it is a Question plus the Choice(s) that belong to that question.