0
0
Djangoframework~30 mins

APIView for custom endpoints in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
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 APIView
Add 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 data
Add 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
1
Import necessary classes
Import APIView from rest_framework.views and Response from rest_framework.response.
Django
Need a hint?

Use from rest_framework.views import APIView and from rest_framework.response import Response.

2
Create GreetingView class
Create a class called GreetingView that inherits from APIView.
Django
Need a hint?

Define class GreetingView(APIView): with no methods yet.

3
Add get method to GreetingView
Inside GreetingView, add a get method that takes self and request parameters and returns Response({"message": "Hello, welcome to the bookstore!"}).
Django
Need a hint?

Define def get(self, request): and return the greeting message using Response.

4
Add URL pattern for GreetingView
In your Django urls.py, import GreetingView and add a URL pattern with path "greet/" that uses GreetingView.as_view().
Django
Need a hint?

Import GreetingView and add path("greet/", GreetingView.as_view()) to urlpatterns.