Building and refining your Django skills requires hands-on practice. These exercises, curated by The Coding College, will help you strengthen your understanding of Django fundamentals and advanced concepts. Follow these challenges to enhance your skills and deepen your Django expertise.
Beginner Level Exercises
1. Create Your First Django Project
- Create a Django project and an app named
blog
. - Configure the
settings.py
file to include your app. - Run the development server and access the default Django homepage.
2. Hello, World! View
- Create a view that returns the text “Hello, World!” when accessed via the
/hello/
URL. - Use Django’s URL dispatcher to route requests to your view.
Hints:
- Define the view in
views.py
. - Map the URL in
urls.py
.
3. Dynamic Greeting
- Modify the
/hello/<name>/
URL to display a personalized greeting. - For example, accessing
/hello/John/
should returnHello, John!
.
Hints:
- Use URL parameters in the
urls.py
file. - Pass the parameter to the view function.
4. Create a Simple Template
- Create a template to display “Welcome to My Django Blog!” in an HTML file.
- Render this template from a view.
Hints:
- Use
render()
to render the template in the view. - Store the template in a
templates
directory.
5. Build a Simple Form
- Create a form that takes a user’s name and displays it back to them on submission.
Hints:
- Use Django’s
forms
module. - Handle form submission in the view and pass the result back to the template.
Intermediate Level Exercises
6. Create a Blog Post Model
- Define a
Post
model with fields liketitle
,content
,author
, anddate_published
. - Make migrations and apply them to the database.
7. Display Blog Posts
- Fetch all posts from the database and display them on a
/posts/
page. - Use the Django ORM and templates to render the posts.
8. Create a Detail View
- Create a detail view for each blog post accessible via
/posts/<id>/
. - Display the
title
,content
, andauthor
of the selected post.
9. Add Pagination
- Add pagination to the blog posts page, showing 5 posts per page.
- Implement navigation links to go to the next or previous pages.
Hints:
- Use Django’s
Paginator
class.
10. User Authentication
- Add login and logout functionality to the site.
- Display a personalized greeting for logged-in users.
Hints:
- Use Django’s built-in
auth
module. - Create templates for login and logout.
Advanced Level Exercises
11. Build a REST API with Django REST Framework
- Create API endpoints to perform CRUD operations on blog posts.
- Implement authentication for API access.
Hints:
- Use Django REST Framework (DRF).
- Install DRF using
pip install djangorestframework
.
12. Add Comment Functionality
- Allow users to add comments to blog posts.
- Create a
Comment
model linked to thePost
model. - Display comments below each blog post.
13. Deploy Your Project
- Deploy your project to a hosting platform like AWS, Heroku, or DigitalOcean.
- Ensure that static files are served correctly.
Hints:
- Use a production-ready database like PostgreSQL.
- Use Django’s
collectstatic
command to manage static files.
14. Add Search Functionality
- Add a search bar to the blog page to search for posts by title or content.
- Implement the search using query filters.
Hints:
- Use the
icontains
field lookup for filtering.
15. Implement User Profiles
- Create a
Profile
model linked to theUser
model using a one-to-one relationship. - Allow users to edit their profile details (e.g., bio, profile picture).
Additional Challenges
- File Uploads: Implement file uploads (e.g., upload images for blog posts).
- Email Integration: Send an email notification whenever a new post is published.
- Custom Admin Panel: Customize the Django Admin interface for managing posts and comments.
Final Thoughts
These exercises are designed to guide you step-by-step through Django’s core features and advanced capabilities. Whether you’re a beginner or an experienced developer, completing these tasks will help you master Django.
For detailed explanations and solutions, visit The Coding College!