Basic APIView Example
A snippet showing how to use APIView in DRF.
# views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Article
from .serializers import ArticleModelSerializer
class ArticleList(APIView):
def get(self, request):
qs = Article.objects.all()
return Response(ArticleModelSerializer(qs, many=True).data)
def post(self, request):
s = ArticleModelSerializer(data=request.data)
if s.is_valid():
s.save()
return Response(s.data, status=status.HTTP_201_CREATED)
return Response(s.errors, status=status.HTTP_400_BAD_REQUEST)
# urls.py
from django.urls import path
from .views import ArticleList
urlpatterns = [
path("articles/", ArticleList.as_view(), name="article-list"),
]
Explanation:
- GET: Returns a list of articles.
- POST: Creates a new article.
- Category Django REST Framework (DRF)
- Total Views 549
- Last Modified 18 July, 2026
- Tags #drf #apiview #views #api
Previous snippet
ModelSerializer Example
Next snippet