In Django REST Framework, you can build API endpoints using either Function-Based Views with the @api_view decorator or Class-Based Views inheriting from APIView. Choosing between them depends on whether your endpoint represents simple procedural logic or an object-oriented controller with reusable behaviors.
@api_viewThe @api_view decorator wraps standard Python functions, giving them DRF superpowers:
request.data.APIViewAPIView organizes HTTP methods into dedicated class methods (get(), post(), put(), delete()), eliminating messy if request.method == "..." branches:
| Feature | @api_view (Functional) | APIView (Class-Based) |
|---|---|---|
| Code Organization | if/elif on request.method | Distinct def get(), def post() methods |
| Inheritance & Mixins | Not supported | Supported (OOP inheritance) |
| URL Registration | path("url/", views.my_view) | path("url/", views.MyView.as_view()) |
| Best Used For | One-off calculations, simple webhooks | Resource endpoints, CRUD logic, OOP reuse |
request.dataUnlike standard Django's request.POST (which only parses form-encoded data), DRF's request.data:
application/json).multipart/form-data, application/x-www-form-urlencoded).request.FILES separately!Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Serializers Overview | Create, Retrieve, Update, Delete APIs |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.