Skip to main content
Backend EngineeringIntermediateFree to access

Django REST API: Validated Task Endpoint

Build a small Django REST Framework task endpoint with input validation, predictable HTTP responses and tests.

Download free Markdown guideNo email, account or payment required · .md text format

What you will build

Understand request validation and API behavior before adding database models and authenticated ownership.

Before you begin

  • Python and Django fundamentals
  • A local Django project and basic HTTP/JSON knowledge

Local setup instructions

  1. Create a virtual environment and install django and djangorestframework.
  2. Start a Django project and app, register rest_framework in INSTALLED_APPS.
  3. Place the starter in app/views.py and map it to a local URL in urls.py.

Starter code

views.py

This is a working starting example, not the finished project. Follow the setup above and complete each milestone to build your own solution.

from rest_framework import serializers, status
from rest_framework.decorators import api_view
from rest_framework.response import Response


class TaskSerializer(serializers.Serializer):
    title = serializers.CharField(max_length=120, allow_blank=False)


@api_view(["GET", "POST"])
def tasks(request):
    if request.method == "GET":
        return Response([{"id": 1, "title": "Plan project"}])

    serializer = TaskSerializer(data=request.data)
    if not serializer.is_valid():
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    # Starter only: persisted storage and auth come in later milestones.
    return Response({"id": 2, **serializer.validated_data},
                    status=status.HTTP_201_CREATED)

Project build milestones

  1. Implement a GET endpoint that returns a sample list of tasks.
  2. Add POST with serializer validation and a clear 400 response for missing title.
  3. Replace in-memory examples with a proper Django model and migrations.
  4. Protect write operations with authentication and user ownership checks.
  5. Write request tests for valid inputs, missing fields and unauthorized writes.

Completion checklist

Use these checks to self-review the finished project.

  • GET returns a JSON list and HTTP 200.
  • Invalid POST returns HTTP 400 with a field error.
  • After authentication is implemented, a user cannot edit another user's tasks.
  • Tests work against a test database and do not depend on production data.

Optional challenges

  • Add pagination, filtering and idempotent task creation.
  • Write OpenAPI documentation and deployment notes without exposing secrets.