A simple Django project can be used as a tutorial for the Django framework and its architecture. The application shows how to connect with PostgreSQL DB and how to store and retrieve data from it.
Create a project called myproject that has a Cars App that includes:
-
Model.py: It has two tables:
- Driver (name and license)
- Car (Make/Model and VIN and foreign key to the driver table)
-
views.py: It has 4 functions to view tables data and to submit new data
-
Template folder as HTML files
-
Static folder for images and CSS files
clone repo and open the command line in the folder directory and install virtualenv and Django
-
Virtual Environment
Download and install virtual environment
pip install virtualenv
Convert the folder to a virtual environment
virtualenv venv
To begin with Django, you first need to activate virtualenv
- The command to activate
venv\Scripts\activate
- The command to deactivate
venv\Scripts\deactivate.bat
- The command to activate
-
Install Django
pip install Django
-
Install PostgreSQL
Install Postgres here and create DB called Car and update the setting.py file with your password then run the following commands in CMD
pip install psycopg2-binary
To provide a connection to PostgreSQL
Py manage.py makemigrations
Py manage.py migrate
To update the project and create tables in model.py to Car DB
-
Run the project
Py manage.py runserver