Having been a frontend developer for years, transitioning to backend development was a bit of a struggle, but a fun one.
I started my journey in tech by learning Python, and it always stuck with me. That's why there was no surprise when I decided to choose Django as the backend framework I wanted to learn.
Starting off, connecting to a database was a challenge for me. It was not easy to make a connection between my Django application and a database during a project I recently worked on. This problem hindered the development process and put the whole application in danger. In this task, I had to create a link connecting Django and PostgreSQL (it had to be done so that data could be fetched out of the storage as well as written in it).
Step-by-Step Solution
Step 1: Identifying the Problem
I examined the error logs and found that connection timeouts were frequent when the application attempted to connect to the database. It meant there was a potential problem with either the database server or the connection string.
Step 2: Setting Up PostgreSQL
I made sure that PostgreSQL was installed and running properly. I used the commands below on my local machine to verify the status and start PostgreSQL service:
sudo service postgresql status
sudo service postgresql start
Step 3: Configuring Django Settings
I then made changes to settings.py in my Django project to set up the correct configuration of the database. This is in the folder named after your project under the main directory of the project. There was also some settings in the form of the database name along with the user, the address of the database server (host) as well as the port number if any.
The following is part of the amended settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'oluwanifemi',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 4: Installing psycopg2
To enable Django to communicate with PostgreSQL, I needed to install the psycopg2 library, which is the PostgreSQL adapter for Python and it was done using pip.
Step 5: Applying Migrations
With the database configuration set up, I applied the migrations to create the necessary database tables.
Step 6: Testing the Connection
To verify that the connection was successful, I created a simple view in Django to interact with the database. I added a new entry to one of the models and retrieved it to ensure data was being stored and fetched correctly.
I enjoyed solving the back-end challenge because it helped me understand that being persistent in my learning process is paramount. That's why I believe that I should move forward with learning through the HNG internship. As such, I feel excited to solve harder puzzles as well as work with experienced developers and those who resemble me in their reasoning practices for lifelong learning. Through the HNG Internship program, my growth potential is limitless due to the opportunity it provides in terms of access to various mentors, enabling me to sharpen these skills in backend development.
I encourage anyone interested in enhancing their technical skills to explore the HNG internship program. You can learn more about the program here and you can also apply for the premium feature here.
Top comments (0)