0
0
Djangoframework~30 mins

Why DRF matters for APIs in Django - See It in Action

Choose your learning style9 modes available
Why DRF Matters for APIs
📖 Scenario: You are building a simple web API to share book information. You want to create the data, configure a setting, write the main logic to expose the data as an API, and complete the setup so the API works properly.
🎯 Goal: Build a basic Django REST Framework (DRF) API that returns a list of books with their titles and authors.
📋 What You'll Learn
Create a list of dictionaries with book data
Add a configuration variable for API version
Use DRF's APIView to create an API endpoint
Complete the URL routing to connect the API view
💡 Why This Matters
🌍 Real World
APIs are how apps and websites share data. DRF makes building APIs in Django easier and faster.
💼 Career
Knowing DRF is important for backend developers working with Django to create clean, maintainable APIs.
Progress0 / 4 steps
1
DATA SETUP: Create the book data list
Create a variable called books that is a list containing these exact dictionaries: {'title': 'The Hobbit', 'author': 'J.R.R. Tolkien'} and {'title': '1984', 'author': 'George Orwell'}.
Django
Need a hint?

Use a list with two dictionaries exactly as shown.

2
CONFIGURATION: Add API version variable
Create a variable called API_VERSION and set it to the string 'v1'.
Django
Need a hint?

Set API_VERSION exactly to 'v1'.

3
CORE LOGIC: Create a DRF APIView to return books
Import APIView and Response from rest_framework.views and rest_framework.response respectively. Then create a class called BookList that inherits from APIView. Inside it, define a get method that takes self and request and returns a Response with the books list.
Django
Need a hint?

Use DRF's APIView and Response to create the API endpoint.

4
COMPLETION: Add URL pattern for the API view
Import path from django.urls. Then create a variable called urlpatterns that is a list containing a single path with the route 'api/v1/books/' (use the API_VERSION variable), the view BookList.as_view(), and the name 'book-list'.
Django
Need a hint?

Use path with the route using API_VERSION and connect it to BookList.as_view().