Skip to main content

Backend Setup

Introduction​

This section will guide you through the setup of the backend of the project. The backend is built using Django and Django REST framework.

The backend is responsible for the API of the web application - Django REST framework.

Prerequisites​

Please make sure you've followed the Prerequisites section before continuing. Python 3.9.x is must installed on your system in order make the backend work.

Python versions and pip versions

If you have both Python 2 and Python 3 installed on your system, make sure you use Python3 and pip3 to any mention of Python and pip in the documentation.

Use the following command to check the version of Python installed on your system:

python --version

If the above command returns Python 3.x.x, then use Python and pip in the documentation as it is.

Setup​

The Django is python based web framework. I've used many third party libraries to make the development process faster and easier. The list of libraries are mentioned inside the requirements.txt file. The requirements.txt file is located inside the /(uni username ) directory (root directory of the project).

.env file for Django

The .env file is used to store the secret keys and other sensitive information. The .env file is located inside the /(uni username )/backend directory. If it is not present, create a new file named .env inside the /(uni username ) /backend directory and copy the contents from the and paste it inside the .env file.

📝 Make sure you have the .env file in the /(uni username )/backend directory.

  • Without SMTP (Email) credentials, Django will not be able to send emails to the users. So, make sure you have the SMTP credentials in the .env file.

Python Virtual Environment Setup​

Create a virtual environment for the project. This will make sure that the project dependencies are installed in the virtual environment and not in the global environment. This will also make sure that the project dependencies are not mixed with the global dependencies which may cause conflicts in the versions of the dependencies.

  1. Open the terminal and navigate to the /(uni username ) directory (root directory of the project).

  2. Run the following command to create a virtual environment:

   python -m venv project-env
  1. Run the following command to activate the virtual environment:
source project-env/bin/activate # For Linux, MacOs and Windows (Git Bash terminal)

# For Windows Users (PowerShell)
project-env\Scripts\activate.bat

(Optional) To deactivate the virtual environment (if needed), run the following command:

deactivate

Django Setup​

Install Dependencies​

  1. Open the terminal and navigate to the /(uni username ) directory (root directory of the project).

  2. Navigate to the /backend directory (this directory contains the manage.py file which is the entry point of the Django project).

  3. Run the following command to install all the dependencies:

pip versions

Make sure you have the latest version of pip installed. If you have an older version of pip installed, run the following command to upgrade pip:

pip install --upgrade pip
pip install -r requirements.txt

Migrate Database (create database tables)​

Django creates the database tables based on the models defined in the Django apps. Projects has 3 apps inside the /apps directory - accounts, placementApplication and placement - that handle the database tables.

  1. Open the terminal and navigate to the /(uni username ) directory (root directory of the project).

  2. Navigate to the /backend directory (this directory contains the manage.py file which is the entry point of the Django project).

  3. Run the following command to create ORM schema of the database tables:

python manage.py makemigrations
  1. Run the following command to create the database tables:
python manage.py migrate

Create Superuser​

We need to create a superuser to access the Django admin panel and to access the API endpoints that admin has access to such as creating Tutor users.

  1. Open the terminal and navigate to the /(uni username ) directory (root directory of the project).

  2. Navigate to the /backend directory (this directory contains the manage.py file which is the entry point of the Django project).

  3. Run the following command to create a superuser:

python manage.py createsuperuser
  1. Enter the username, email and password of the superuser when prompted.

Run the Django Server​

  1. Open the terminal and navigate to the /(uni username) directory (root directory of the project).

  2. Navigate to the /backend directory (this directory contains the manage.py file which is the entry point of the Django project).

  3. Run the following command to run the Django server:

python manage.py runserver

By default, the Django server runs on port 8000. You can access the Django server at http://localhost:8000/ but in this project, we are using the frontend built using React which runs on port 3000. So, we need to run the Django server on port 8000 and the React server on port 3000.