How to Start a Django Project with a Database(PostgreSQL)?

Introduction

Django is a web framework which is made using python and improves the design of the project which is done by the developers who are experienced in this field. It is very important to know about web development because you are ready to make a project with Django which is open source and free to use. It is extremely fast since it is made for the developers to complete their task as fast as possible. It provides security in a serious manner and does not make any mistakes with the developers. If you are interested and ready to make a project in Django using PostgreSQL database, you can join PostgreSQL Training course and create your project today itself.

Now, Let’s see how to create a project using Django which is connected to the PostgreSQL database. 

Prerequisites

  • Know the basics and important concepts of Django and do whatever the steps are taken in this article in order to start a project.
  • You must be familiar with the Python language if you haven’t used Django before.
  • You have to know about the PostgreSQL database and how to connect it to Django.

Tools and Software Required

  • Windows (Operating System)
  • PostgreSQL (Database)
  • Python Version 3.7
  • Any Text Editor
  • Any Database GUI

In this blog, we are going to use Windows operating systems, if you use different operating systems like linux, mac, parrot, etc. There will be small changes in using some commands. It is very simple for other operating system users to complete the task by using the internet and getting the information about that operating system’s commands.

Let’s start our project

Step 1: Create a Virtual environment and directory

  • Create a directory for the project and name it as “sample”.

mkdir sample

  • Connect to the sample directory.

cd sample

  • Create a virtual environment for this directory.

python -m venvir envir

  • Now, you have to activate this virtual environment.
  • Then, Run the command mentioned below.

envir/scripts/activate

envir is nothing but the name of the environment where we are using a short name instead of using environment we made it short as “envir”.

Step 2: Installation of Django

After the completion of the creation of the directory and the virtual environment then it is the time for the Django installation. To install Django use the following command:

Pip install django

After that, Django will be installed, which will have the latest version available in the market.

Step 3: Text Editor

Open any text editor that you are using like visual studio code, pycharm, and many more.

  • Open any folder and import the directory that we have created earlier.
  • Then open the terminal of the text editor which helps you to run and compile your code in the same place.
  • Now, you have to activate the virtual environment in that text editor.

envir/scripts/activate

  • Click on enter then it shows that the virtual environment is activated.

Step 4: Create a Django project

And now we are ready to start our project using Django which will be having a single project in the project directory yet you can add any number of apps that you wanted to

Django-admin startproject

sampleTesting

Here, we have our project which is named “sampleTesting”.

Step 5: Use Django app

  • In the text editor terminal, use cd to the project directory.

cd sampleTesting

  • Now, you have to run the command to start the django app.

python manage.py startapp testdb

When the app is completely built then you have to make your required table which was previously designed in order to manage the data.

  • Then, you have to run the server to check that it is working properly or not.

python manage.py runserver

The above mentioned command is used to run Django which will be used many times while doing the project.

Step 6: Setting database server

  • Open pgAmin6.
  • Then click on the server and click PostgreSQL version to check the version of it.
  • Now, create a database and name it.
  • And now you have to give a name for the database and name it as “db1” which is used for the testing.
  • Finally, creation of the database is completed.

Step 7: Come to the coding part to configure the database

Come back to coding part

  • Click on settings.py
  • There will be a database config part present in the code in between 75 and 95 lines of the code.
  • Now, it shows some code which is used for the database that is defaultly taken.
  • Edit the code and mention the details that are required here.

Step 8: creation of a table

Come back to your app in the models.py

  • Create a table by directly writing a class to use the database without writing any raw SQL queries.

Step 9: Transfer the table to the database (PostgreSQL)

To send the table to the PostgreSQL database, we have to follow the following steps:

  • maketransfer: To make some changes in the output we have to modify the things present in it.
  • transfer: It is the final step used to submit the table into our PostgreSQL database.

Now, we have to run this maketransfer which can be used in the coding as makemigrations which is given below:

python manage.py makemigrations

Now, you will see that there is “No change detected” since it is not registered with our app.

  • Click on settings.py to register the app

Now, you will get no errors with the app, yet you will see something and it is “No module named psycopg2”.

This error occurred because we didn’t install thi psycopg2 library which is used to make connections with Django and the PostgreSQL database. And we can install this library as mentioned below:

Pip install psycopg2

Now, we have to run our migrations again since everything is good in this.

python manage.py makemigrations

The final stage is running “migrate”

python manage.py migrate

  • Now, you have to go back to the pgAdmin.
  • Now, double click on the tables in the paAdmin.
  • It shows us that the PostgreSQL database is created and now transfers the data tables from the Django project to the database.
  • Now, some tables will be displayed on the screen.
  • Select any table that you wanted to select and check whether it is working in a proper manner or not.

Now, Right click on that table and click on “View or Edit data” and then select “All rows”

That table will be empty because we didn’t add any kind of data to it. If we add any data then it will show that data.

Congratulations !! 

We have successfully completed creating a Django project with the PostgreSQL database.

Conclusion

In this blog, we have discussed the creation of a project using Django with the PostgreSQL database. It included 9 steps to complete the project. Also, here we have learnt about the installation of several softwares and libraries that are required to us. Since Django is an open source tool to complete your projects most of the people use this software and connect it to PostgreSQL database.

Author Bio:  Sai, Postgraduate in Marketing, Digital Marketing professional at HKR Trainings. I aspire to learn new things to grow professionally. My articles focus on the latest programming courses and E-Commerce trends. 

Leave a Reply

Your email address will not be published. Required fields are marked *