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
Tools and Software Required
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
mkdir sample
cd sample
python -m venvir envir
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.
envir/scripts/activate
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
cd sampleTesting
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.
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
Step 7: Come to the coding part to configure the database
Come back to coding part
Step 8: creation of a table
Come back to your app in the models.py
Step 9: Transfer the table to the database (PostgreSQL)
To send the table to the PostgreSQL database, we have to follow the following steps:
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.
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, 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.
In my previous article I have given details about application support engineer day to day…
In my previous articles I have given the roles and responsibilities of L1,L2 and L3…
In my previous articles i have given the hierarchy of production support in real company…
In this article i would like to provide information about production support organization structure or…
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…