I would suggest to make use of virtual environments to run any kind of application. (I personally use pipenv)
requirements.txt
can be found at top level directory of the project folder.
Windows Users
pip install pipenv
Mac/Linux Users
pip3 install pipenv
Use pipenv shell
to create a virtual environment. Once, the environment is created.
If you only have a requirements.txt
file available when running pipenv install
, pipenv will automatically import the contents of this file and create a Pipfile
for you.
You can also specify pipenv install -r path/to/requirements.txt
to import a requirements file.
Creating a PostgreSQL database involves several steps, including installing PostgreSQL, accessing the PostgreSQL command line, and executing SQL commands. Here's a general guide:
-
Linux:
sudo apt-get update sudo apt-get install postgresql postgresql-contrib
-
macOS:
brew install postgresql
-
Windows: Download and install PostgreSQL from the official website: PostgreSQL Downloads
-
Linux & macOS: Open a terminal and access the PostgreSQL command line using the
psql
command.psql -U postgres
-
Windows: Open the SQL Shell (psql) from the PostgreSQL installation in your Start menu.
CREATE DATABASE yourdatabasename;
Replace yourdatabasename
with the desired name for your database.
\c yourdatabasename
This command connects to the database you just created.
CREATE USER yourusername WITH PASSWORD 'yourpassword';
Replace yourusername
and yourpassword
with the desired username and password for your database user.
GRANT ALL PRIVILEGES ON DATABASE yourdatabasename TO yourusername;
\q
This command exits the PostgreSQL command line.
psql -U yourusername -d yourdatabasename -h localhost
Replace yourusername
and yourdatabasename
with your actual username and database name.
Now, you have successfully created a PostgreSQL database, user, and connected to the database.
Make sure you have installed python-dotenv
package before following the below steps:
Fill in the details that you created in the above steps 3 and 5
DB_NAME = "YOUR_DATABASE_NAME"
DB_USER = "YOUR_DATABASE_USER_NAME"
DB_PASSWORD = "YOUR_DATABASE_PASSWORD"
In btre/settings.py, add the following code:
from dotenv import dotenv_values
config = dotenv_values(".env")
And below in the database section add the following:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config["DB_NAME"],
"USER": config["DB_USER"],
"PASSWORD": config["DB_PASSWORD"],
"HOST": "127.0.0.1",
"PORT": "5432",
}
}
To run the application, activate the virtual environment and then write
python manage.py runserver
Congratulations! 🎉 Your Django application is ready and running @ http://127.0.0.1:8000/