Django Knowledgebase
Installation
pip install django
Commands
django-admin startproject <project-name> - Create a Django project
python manage.py runserver - Run the Django dev server
python manage.py makemigrations - Create Migration Scripts
python manage.py migrate - Migrate Database
File Structure
project-name/ ← higher level folder
|-- project-name/ ← django project folder
| |-- project-name/
| | |-- init.py
| | |-- settings.py
| | |-- urls.py
| | |-- wsgi.py
| +-- manage.py
Five main files of the base project are as follows:
manage.py - Used to run management commands related to the project [migrations, run server, etc]
init.py - Empty file to indicate Python that this is a package
settings.py - Whole projects configuration file
urls.py - Maps all the requests to the routes/paths
wsgi.py - Used for deployment
A Django project can have multiple apps for different functionalities in it.
In this file tree, we have created an app called sampleapp
project-name/
|-- project-name/
| |-- sampleapp/ <-- our new django app!
| | |-- migrations/
| | | +-- __init__.py
| | |-- __init__.py
| | |-- admin.py
| | |-- apps.py
| | |-- models.py
| | |-- tests.py
| | +-- views.py
| |-- project-name/
| | |-- __init__.py
| | |-- settings.py
| | |-- urls.py
| | |-- wsgi.py
| +-- manage.py
Creating Databse in PostgreSQL
createdb -U {{username}} -h localhost --owner={{username}}--lc-ctype='en_US.UTF-8' --lc-collate='en_US.UTF-8' --encoding='UTF8' -e {{db name}};
Copying Database in PostgreSQL
CREATE DATABASE {{new_db}} WITH TEMPLATE {{original_db}} OWNER {{username}};
Dropping Database in PostgreSQL
dropdb -U {{username}} -h localhost {{db_name}};
Adding Table in PostgreSQL
ALTER TABLE table_name ADD COLUMN new_column_name data_type;
Copy Data from one column to another
UPDATE {{table_name}}
SET {{column1}} = {{column2}}
Dropping Table in PostgreSQL
DROP TABLE {{table_name}};
Dropping Column in PostgreSQL
ALTER TABLE {{table_name}}
DROP COLUMN {{column_name}};
Import CSV File Into PosgreSQL Table
Example
CREATE TABLE persons ( id serial NOT NULL, first_name character varying(50), last_name character varying(50), dob date, email character varying(255), CONSTRAINT persons_pkey PRIMARY KEY (id) )
Change DB to Postgres
In your projects setting.py, change the following
. . .DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
. . .
to
. . .DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': '{{db name}}', 'USER': '{{username}}', 'PASSWORD': '{{password}}', 'HOST': 'localhost', 'PORT': '', # Leave it empty to use default - 5432 } }
. . .
After changing the backend,
python manage.py makemigrations - Create the migration scripts
python manage.py migrate - Run the migration scripts
Creating Python Virtual Environment
python3 -m venv /path/for/virtual/environment