Building a Python CRUD Rest API with Django, Django Rest Framework, Postgres, Docker, and Docker Compose

Posted by

Python CRUD Rest API with Django, Django Rest Framework, Postgres, Docker, and Docker Compose

Python CRUD Rest API with Django, Django Rest Framework, Postgres, Docker, and Docker Compose

If you are looking to build a Rest API in Python, using Django, Django Rest Framework, Postgres, Docker, and Docker Compose is a great option. This article will guide you through the process of setting up and using these tools to create a powerful and scalable Rest API.

Setting Up Django and Django Rest Framework

To get started, you will need to install Django and Django Rest Framework. You can do this by running the following commands in your terminal:

“`html

pip install django
pip install djangorestframework
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
```

Once you have Django and Django Rest Framework installed, you can begin building your Rest API. First, you will need to define your models in the models.py file of your Django app. This will allow you to create, read, update, and delete data from your database using the Rest API.

Setting Up Postgres

To use Postgres as your database for your Rest API, you will need to install and configure it. You can do this by running the following commands in your terminal:

“`html

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
sudo -u postgres psql
CREATE DATABASE mydatabase;
CREATE USER myuser WITH PASSWORD 'mypassword';
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
```

Once Postgres is installed and configured, you can update your Django settings to use Postgres as your database. You will need to add the following configuration to your settings.py file:

“`html

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '',
    }
}
```

Setting Up Docker and Docker Compose

To containerize your Rest API and make it easy to deploy, you can use Docker and Docker Compose. First, you will need to install Docker and Docker Compose on your machine. You can do this by following the installation instructions on the Docker website.

Once you have Docker and Docker Compose installed, you can create a Dockerfile and a docker-compose.yml file in your Django project to define your application’s container and its dependencies. These files will allow you to easily build and run your Rest API in a containerized environment.

“`html

FROM python:3
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
```

“`html

version: '3'

services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    environment:
      - POSTGRES_DB=mydatabase
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypassword
```

Conclusion

By using Django, Django Rest Framework, Postgres, Docker, and Docker Compose, you can create a powerful and scalable Rest API in Python. These tools provide a robust and efficient way to build and deploy Rest APIs, making it easier to develop and maintain your applications.

By following the steps outlined in this article, you can set up and use these tools to build a Rest API that meets your specific needs and requirements. Whether you are building a small personal project or a large-scale enterprise application, Django, Django Rest Framework, Postgres, Docker, and Docker Compose provide a flexible and powerful platform for Rest API development in Python.

0 0 votes
Article Rating
13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@emtiazahmed5333
11 months ago

i cant connect my database still can u help me..?

@salmanakhtar8043
11 months ago

Its an excellent tutorial and easy to follow.
You are doing great work, Francesco.

@jairajsahgal7101
11 months ago

Thank you

@thenewway416
11 months ago

How to this for MYSQL

@tausiqsamantaray
11 months ago

How you know all of these things node, express, flask, django, go, rust. Salute to you.

@MoisesVillalbaSilvero
11 months ago

Thank you!!!! Excellent tutorial..

@insteadtechsolutions4276
11 months ago

(dockvenv) PS D:dockerdjangowin> docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

17dcab655689 postgres:12 "docker-entrypoint.s…" 57 minutes ago Up About a minute 0.0.0.0:5432->5432/tcp db

(dockvenv) PS D:dockerdjangowin> docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

djangowin-djangoapp latest 72ad0afdefaa 51 seconds ago 207MB

postgres 12 7ba1194383d3 13 days ago 406MB

@giuliabaniqued9774
11 months ago

😍👏🏼

@Hamid-mf4qz
11 months ago

Thank you, my brother. This video tutorial solved my problem

@Jannerparejagutierrez
11 months ago

Me gusta tu video, el problema es que inicias con una carpeta "live" que ya contiene unos archivos y no dices de donde los sacaste. Empezando por ahí ya es algo que no ayuda mucho a seguir tu video.

@SuperSalacho
11 months ago

Great video Francesco, thank u so much

@boukraailyesali9131
11 months ago

Great job! thank you

@francescociulla
11 months ago

https://francescociulla.com/

0:00 Intro, requirements, project creation

4:40 app configuration

10:43 model, serializer, controller, URLs

21:15 bash script

23:48 Dockerfile, docker-compose.yml file

29:40 run the Postgres container, build the Docker image, run Django app container

35:27 Test the application