Module 10 / Project 05 — Complete App¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
A full CRUD application combining models, views, templates, Django REST Framework, authentication, and tests.
Why this project exists¶
This project ties together everything from Projects 01 through 04 into a single, complete application. It demonstrates how Django's components work together in a real project: models define the data, views handle logic, templates render HTML for browsers, DRF serializers serve JSON for API clients, and Django's auth system controls access.
The addition of tests is critical. Django includes a test framework built on Python's unittest that provides a test client for simulating HTTP requests, database isolation between tests, and helpers for common assertions. Writing tests ensures your application works correctly and continues to work as you make changes.
Run¶
cd projects/modules/10-django-fullstack/05-complete-app
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Then open your browser to:
- http://127.0.0.1:8000/books/ — browse all books (HTML)
- http://127.0.0.1:8000/books/add/ — add a book (requires login)
- http://127.0.0.1:8000/api/books/ — REST API (JSON, browsable)
- http://127.0.0.1:8000/api/authors/ — REST API for authors
- http://127.0.0.1:8000/admin/ — Django admin interface
- http://127.0.0.1:8000/register/ — create a user account
- http://127.0.0.1:8000/login/ — log in
To run the tests:
Press Ctrl+C to stop the server.
Expected output¶
Running python manage.py test shows:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..........
----------------------------------------------------------------------
Ran 10 tests in 0.045s
OK
Destroying test database for alias 'default'...
The application serves both an HTML interface (for browsers) and a JSON API (for programmatic access). Both share the same models and database.
Alter it¶
- Add an
edit_bookview and template. The URL should be/books/<pk>/edit/. UseBookForm(request.POST, instance=book)to pre-fill and update. Protect with@login_required. Add a test for it. - Add a
delete_bookview with a confirmation template. Add a test that verifies deletion works and that anonymous users cannot delete. - Add a
/api/books/<pk>/reviews/nested endpoint using DRF. Create aReviewmodel withbook(ForeignKey),rating(1-5), andtextfields. Write tests for the new API.
Break it¶
- In
tests.py, change anassertEqualassertion to check for the wrong value. Run the tests. Read the failure output and understand what Django tells you. - Remove
select_related("author")from theBookViewSetqueryset. The app still works, but run the tests — do they still pass? (Yes, but performance degrades silently. This is why N+1 query detection tools exist.) - Comment out the CSRF middleware in
settings.py. Submit a form. What changes?
Fix it¶
- Fix the assertion back to the correct value. Test failure output shows the expected vs actual values, which makes debugging straightforward.
- Add
select_related("author")back. The tests pass either way because correctness is not affected — only performance. In production, tools likedjango-debug-toolbarornplusonedetect these issues. - Uncomment the CSRF middleware. Without it, your site is vulnerable to Cross-Site Request Forgery attacks. The middleware and
{% csrf_token %}work together to verify that POST requests originate from your site.
Explain it¶
- How does Django's test framework create an isolated database for each test run? What happens to data created during tests?
- What is the purpose of
self.clientin Django tests? How does it differ from a real browser? - How do the HTML views and the DRF API views share the same models? What are the advantages of serving both interfaces?
- Looking at the full application, what would you need to add to make it production-ready? (Think: security, deployment, performance.)
Mastery check¶
You can move on when you can:
- build a Django app from scratch with models, views, templates, and DRF,
- write tests for models, views, and API endpoints,
- explain how Django's components fit together (MTV + DRF + auth),
- describe what makes this a "full-stack" application.
Related Concepts¶
Next¶
Congratulations on completing Module 10! You now have full-stack Django skills.
Return to the Module Index to choose your next module.