APIView for custom endpoints
📖 Scenario: You are building a simple Django REST API for a bookstore. You want to create a custom endpoint that returns a greeting message.
🎯 Goal: Create a Django APIView with a custom get method that returns a JSON response with a greeting message.
📋 What You'll Learn
Create a Django view class called
GreetingView that inherits from APIViewAdd a
get method to GreetingView that returns a JSON response with {"message": "Hello, welcome to the bookstore!"}Use Django REST framework's
Response class to return the JSON dataAdd a URL pattern for
GreetingView at path "greet/"💡 Why This Matters
🌍 Real World
Custom API endpoints let you create tailored responses for your web or mobile apps, like greeting messages or data queries.
💼 Career
Knowing how to build APIViews is essential for backend developers working with Django REST framework to create flexible APIs.
Progress0 / 4 steps