# Django REST API: Validated Task Endpoint

TechBuilt Open School — Free, self-guided technical project kit

- **Track:** Backend Engineering
- **Level:** Intermediate
- **Access:** No account, signup, payment or enrollment required
- **Format:** Guided plan and runnable starter example, not a complete solution or a live class
- **Online guide:** https://techbuiltos.online/projects/django-rest-task-api

## Project goal

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

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

## Before you begin

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

## Local setup

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 example — views.py

The sample below is a starting point. It may require the prerequisites above. Complete the subsequent milestones to finish your own project.

```python
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)
```

## 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

- [ ] 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 extensions

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

## Related learning

- Individual course roadmap: https://techbuiltos.online/courses/rest-apis
- Individual course roadmap: https://techbuiltos.online/courses/postgresql

- Specialization: https://techbuiltos.online/specializations/backend-developer
- Specialization: https://techbuiltos.online/specializations/full-stack-developer
- Specialization: https://techbuiltos.online/specializations/python-developer

---

This original self-study project is independent of paid cohorts. Review any external library documentation before use. Do not commit real credentials, private client files or personal data to public repositories.
