DEV Community

Cover image for Python CRUD Rest API, using: Django, Postgres, Docker and Docker Compose

Python CRUD Rest API, using: Django, Postgres, Docker and Docker Compose

Francesco Ciulla on April 02, 2023

Let's create a CRUD Rest API in Python using: Django (Python framework) Django Rest Framework (for the Rest API) Postgres (relational database) D...
Collapse
 
alok38 profile image
alok-38 • Edited

Update:

I get the same error even after creating the database and a table in PostgreSQL and running

docker compose build

. Below is the output of

docker compose up

.

 docker compose up
Attaching to db, django_app
db          | 
db          | PostgreSQL Database directory appears to contain a database; Skipping initialization
db          | 
db          | 2023-06-09 11:43:08.562 UTC [1] LOG:  starting PostgreSQL 12.15 (Debian 12.15-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db          | 2023-06-09 11:43:08.563 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db          | 2023-06-09 11:43:08.563 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db          | 2023-06-09 11:43:08.564 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db          | 2023-06-09 11:43:08.580 UTC [27] LOG:  database system was shut down at 2023-06-09 11:32:17 UTC
db          | 2023-06-09 11:43:08.587 UTC [1] LOG:  database system is ready to accept connections
django_app  | Creating Migrations...
django_app  | Migrations for 'django_app':
django_app  |   django_app/migrations/0001_initial.py
django_app  |     - Create model User
django_app  | ====================================
django_app  | Starting Migrations...
django_app  | Operations to perform:
django_app  |   Apply all migrations: admin, auth, contenttypes, django_app, sessions
django_app  | Running migrations:
django_app  |   No migrations to apply.
django_app  | ====================================
django_app  | Starting Server...
django_app  | Watching for file changes with StatReloader
django_app  | Performing system checks...
django_app  | 
django_app  | System check identified no issues (0 silenced).
django_app  | June 09, 2023 - 11:43:10
django_app  | Django version 3.2.5, using settings 'django_project.settings'
django_app  | Starting development server at http://0.0.0.0:8000/
django_app  | Quit the server with CONTROL-C.
django_app  | Not Found: /
django_app  | [09/Jun/2023 11:43:13] "GET / HTTP/1.1" 404 2171
Enter fullscreen mode Exit fullscreen mode
Collapse
 
francescoxx profile image
Francesco Ciulla

did you manage to solve it?

Collapse
 
alok38 profile image
alok-38

Not yet. If I try this once more and face any issues, I will update.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

ok good luck

Collapse
 
alok38 profile image
alok-38 • Edited

Hi Francesco,

First of all, a huge thanks for for putting in so much effort in creating this comprehensive and easy to follow step-by-step tutorial.

I was able to successfully run

docker compose build

but when I run

docker compose up

I get this error

django error

docker compose up

I ran

docker compose down

checked the logs for errors and I could not find any.

Collapse
 
francescoxx profile image
Francesco Ciulla

did you check the video on YouTube?

Collapse
 
alok38 profile image
alok-38

Not yet. I will do that some other day. Thank you.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

no problem

Collapse
 
svobop profile image
svobop

Cool.

I tried this to learn a bit about docker. I don't think I understand how it is supposed to work.

When I first went through the steps I've got an error:

djangoapp  |   File "/app/djangoproject/urls.py", line 21, in <module>
djangoapp  |     path('users/', include('djangoapp.urls')),
djangoapp  | NameError: name 'include' is not defined
Enter fullscreen mode Exit fullscreen mode

Which is pretty obvious, I forgot to import include, my issue is I don't know how to update the code in the container.

I would expect rebuilding should fix that, but it doesn't.

docker compose up --build
Enter fullscreen mode Exit fullscreen mode

I have also tried to remove everything and retrace my steps.

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)

docker system prune -af --volumes
Enter fullscreen mode Exit fullscreen mode
Collapse
 
francescoxx profile image
Francesco Ciulla

line 2. Did you import the include?

Image description

Collapse
 
svobop profile image
svobop

That's exactly how I tried to fix that. I have that code in my project folder, but no matter what I do the container seems to be stuck with the old code.

Do I need to change Dockerfile or docker-compose.yml to make sure files are overwritten when rebuilding?

Thread Thread
 
francescoxx profile image
Francesco Ciulla

did you try docker compose up --build?

Thread Thread
 
svobop profile image
svobop

Yes, that's what I said would expect to update the container.

That's my luck. I always run into some weird bugs. I will have to try later on my other pc.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

I'd suggest to try again from scratch and when you find the error you let me know here. good luck

Collapse
 
sirexlangnmn profile image
Federex A Potolin • Edited

I have encountered some issues.

1.) I used Dbeaver to have an interface for my postgres database. But I got connection error says FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"

2.) The moment I run docker compose up I dont see output in terminal that is similar to the content of django.sh file.

I am using a windows with linux subsysmtem using ubuntu. I run the djangoproject in ubuntu 22.04 linux subsystem.

But your project tutorial works fine if I use the standard procedure or standalone django and postgre, without using Docker.
But if I use docker, I encountered the error I said earlier

Collapse
 
francescoxx profile image
Francesco Ciulla

hi. for point one, it really seems like a wrong password issue, beecause it's trying to authenticate.

Did you check the youtube video?

Collapse
 
rezar12 profile image
rezar12

thank

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
trimbleava profile image
Ava Trimble

Thank you very much for the post. It was very helpful.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Ava

Collapse
 
obijerome profile image
Obi

Nice 👍
Why not use a Django Rest Framework ModelviewSet instead?
With pagination too. 😁
Btw, you said you would use Python 3.10, not 3.7 😉

Collapse
 
francescoxx profile image
Francesco Ciulla

I just used this simple one.

you are partially correct, because I used Python 3.10 on my machine, that's the requirement just to run the django-admin CLI command

you odn't really need python 3.7 because you tun it inside a docker container.

I hope this was clear