This project is a feature-rich Django baseplate designed to serve as the foundation for web applications. The goal is to provide a streamlined, quick-to-deploy Django environment, allowing you to focus more on building your application rather than setting up the infrastructure. Over time, this project will be expanded to include additional features. Contributions are welcome!
- Django – The main framework handling everything.
- Django REST Framework – API framework that enables serices to commincate with the webapp through API endpoints.
- Celery – A task queue for handling background jobs.
- Redis – A message broker between Celery and Django.
- PostgreSQL – The primary database for the Django instance.
- Nginx – Serves static content and acts as a reverse proxy.
- Basic user handling (login, registration, profile editing, logout).
- Custom authentication backend.
- API capabilities for easy communication with other services.
- Background task execution, offloading computation-intensive tasks while keeping the website responsive.
-
Clone the repository.
-
Edit the
.envfile to match your desired configurations. Make sure to update all secrets and passwords before deployment. -
Run the following command from the directory where your
docker-compose.ymlis located:docker compose up -d --build
-
Start the containers:
docker compose up -d --build
-
Stop the containers:
docker compose down
-
Stop containers and remove the database:
docker compose down -v
-
Create a superuser:
docker exec -it django python manage.py createsuperuser -
Create a new Django app:
python createapp.py <app_name>
To create a new API endpoint:
- Add a new view to
views.pyusing the@api_viewdecorator. - Register the view in
urls.py. - If you are returning data from model objects, use serializers to ensure proper formatting.
- For data modifications, use serializers to validate incoming data, and then save the object.
Example:
File: viesw.py
@api_view(["GET"])
def example_get(request):
data = ExampleModel.objects.all()
serializer = ExampleSerializer(data, many=True)
return Response(serializer.data)
@api_view(["POST"])
def example_post(request):
serializer = ExampleSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)Celery enables background task processing without stalling the website.
To create a background task:
- Add a new function in
tasks.pyand decorate it with@shared_task(bind=True). This makes the function executable by Celery workers instead of the main Django instance. - Call the function asynchronously using
.delay().
To monitor task progress, retrieve the task ID using self.request.id inside the task function. You can store this ID and check the status by querying django_celery_results.models.TaskResult.
Example:
File: task.py
@shared_task(bind=True)
def example_task(self):
id = self.request.id
# Your task logic here
return "Task Completed"File: viesw.py
# Call this task
example_task.delay()