Follow this guide to set up the project:
git clone https://github.com/ISPP-G5/NexONG_Backend.git
in the directory of your choice.pip install virtualenv
.python3 -m venv myenv
, myenv being what you want to name the virtual environment.source venv/bin/activate
.You can create the Windows virtual environment by running commands as in Linux following this guide. However, I think the easiest option is creating it through VSCode.
ctrl+shift+p
on your keyboard.Python: Select Interpreter
.+ Create Virtual Environment
.Venv
.Python 3.11
OK
.(.myenv)
on the terminal, something like this:pip install -r requirements.txt
in the project's root folder.sudo apt install postgresql
.sudo su - postgres
.psql -c "create user nexong with password 'nexong'"
.psql -c "create database nexongdb owner nexong"
.psql -c "ALTER USER nexong CREATEDB"
.C:\Program Files\PostgreSQL\16\bin
and execute psql -U postgres
.CREATE USER nexong WITH PASSWORD 'nexong';
.create database nexongdb owner nexong;
.ALTER USER nexong CREATEDB;
. You can check if the database is correctly created using \l
in the psql instance
In the root folder of the project, run:
python manage.py makemigrations nexong
python manage.py migrate
python manage.py loaddata populate.json
In the root folder of the project, run:
python manage.py runserver
http://127.0.0.1:8000/api/
To consult the API's Swagger documentation you can check it in http://127.0.0.1:8000/docs
while the app is running.
It's not unusual for the database to change. This causes an error, which usually, looks like this:
This happens because the migrations that were used to create the DB don't apply anymore. The solution is quite straightforward. Instead of executing the same commands every time and waste time, let's make shell scripts:
.vscode
in the root folder. It should look like this:drop_db.sh
inside .vscode
and paste this inside it:
#!/bin/bash
DB_NAME="nexongdb"
echo "Please enter your sudo password:" read -s SUDO_PASSWORD
echo "$SUDO_PASSWORD"
sudo -u postgres psql -c "DROP DATABASE IF EXISTS $DB_NAME;" sudo -u postgres psql -c "create database $DB_NAME owner nexong" sudo -u postgres psql -c "ALTER USER nexong CREATEDB"
echo "Database '$DB_NAME' has been deleted successfully."
- Then, create another file in the same folder as `drop_db.sh` called `migrations.sh` and paste the following code inside it:
```bash
#!/bin/bash
# Install requirements
pip install -r requirements.txt
# Delete previous migrations
rm -rf nexong/migrations
# Make migrations
python manage.py makemigrations nexong
# Migrate
python manage.py migrate
# Create superuser
python manage.py createsuperuser
#Loaddata
python manage.py loaddata populate.json
# Run server
python manage.py runserver
.vscode
directory:
chmod +x drop_db.sh
chmod +x migrations.sh
drop_db.sh
script and then the migrations.sh
script.
To run them, go to the .vscode
directory, execute drop_db.sh
or migrations.sh
in the terminal.drop_db.bat
inside .vscode
and paste this inside it:
@echo off
cd /d "C:\Program Files\PostgreSQL\16\bin"
echo Changing directory to PostgreSQL bin folder...
echo.
set PGPASSWORD=your_postgres_password
echo Dropping existing PostgreSQL database if it exists... echo. psql -U postgres -c "DROP DATABASE IF EXISTS nexongdb;" echo.
echo Creating PostgreSQL database... echo. psql -U postgres -c "CREATE DATABASE nexongdb WITH OWNER nexong;" echo.
echo PostgreSQL database created successfully.
cd /d "C:\Your_backend_project_root" echo Changing directory to the project root... echo.
- Then, create another file in the same folder as `drop_db.bat` called `migrations.bat` and paste the following code inside it:
```bash
@echo off
echo Going back one directory...
cd ..
echo Deleting "migrations" folder in "/root/nexong"...
rd /s /q "nexong\migrations"
echo Installing requirements...
pip install -r requirements.txt
echo Running "python manage.py makemigrations nexong"...
python manage.py makemigrations nexong
echo Running "python manage.py migrate"...
python manage.py migrate
echo Running "python manage.py loaddata populate.json"...
python manage.py loaddata populate.json
echo All steps completed successfully.
Note: if you want you can add python manage.py runserver
at the end of the script or python manage.py createsuperuser
after the populate command
drop_db.bat
script and then the migrations.bat
script.
To run them, go to the .vscode
directory, execute drop_db.bat
or migrations.bat
in the terminal.In this way, the usual problem of DB modification is solved.