Django Project Example
- 13 Aug 2018, migrate to Django 2.1, adjust some code, add some
READMEcontent.
- Setup Project & Apps
- Model with all common used data type
- Web form CRUD single table
- User Authenticaton
- Model association & One-to-Many Web form CRUD
- Login, Logout, Remember Password, Session.
With Anaconda Distribution
# Example: conda create --name <ve-name> <package> conda create --name mydjango django # to activate virtual environment source activate mydjango # to deactivate virtual environment source deactivate mydjango # to remove virtual environment conda remove -n mydjango -all # to list all available virtual environment conda info --envs
With Python Virtual Env
- Create virtual environment, execute:
mkvirtualenv <env-name>Example:
mkvirtualenv djangoEnv- activate virtual environment:
workon djangoEnv- deactivate virtual environment:
deactivate- references:
Listing all current Virtual Env Library & Version
pip freeze --localor
pip list
- Install django through pip, execute cmd:
pip install Django - Check installed django version, execute cmd:
python -m django --version - Creating a new project, execute cmd:
django-admin startproject <project-name> - Creating a new apps, execute cmd:
python manage.py startapp <app-name> - To run server, execute cmd:
python manage.py runserver - Run server with custom port, execute cmd:
python manage.py runserver 9696 - Run django command shell, execute cmd:
python manage.py shell
- Open file
<project-name>/settings.py - Search for
DATABASEpythons dictionary variable. - Default
ENGINEis SQLite:django.db.backends.sqlite3 - Another options for
ENGINE:- PostgreSQL:
django.db.backends.postgresql - MySQL/MariaDB:
django.db.backends.mysql - OracleDB:
django.db.backends.oracle - MS SQL Server
- Firebird
- PostgreSQL:
- For another db engine than SQLite, there're other variable, for example:
DATABASES= { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'mydatabaseuser', 'PASSWORD': 'mypassword', 'HOST': '127.0.0.1', 'PORT': '5432', } }
- To make a new migration of changes, execute cmd:
python manage.py makemigrations - To make a new migration by certain apps, execute cmd:
python manage.py makemigrations <apps-name> - To check out the underlying SQL of migrations, execute cmd:
python manage.py sqlmigrate <apps-name> <version> - To apply database changes, execute cmd:
python manage.py migrate - Time zone setup, look for
TIME_ZONEvariable, list of TZ available Here.TIME_ZONE='Asia/Jakarta'
- Create an admin account, execute cmd:
python manage.py createsuperuser
- Common used MySQL database field to Django data type:
| MySQL Field | Django Model Field | Parameters |
|---|---|---|
VARCHAR | CharField | max_length, unique, null, blank, default, primary_key |
VARCHAR | EmailField | null, blank, default |
VARHCAR | URLField | null, blank, default |
LONGTEXT | TextField | null, blank, default |
INT | IntegerField | null, default |
DECIMAL | DecimalField | null, default |
TINYINT / BOOLEAN | BooleanField | null, default |
DATE | DateField | null, default |
TIME | TimeField | null, default |
DATETIME | DateTimeField | null, default |
LONGBLOB | BinaryField | null, default |
- Django Models database association data type:
ForeignKey, a field type that allows us to create a one-to-many relationship.OneToOneField, a field type that allows us to define a strict one-to-one relationship.ManyToManyField, a field type which allows us to define a many-to-many relationship.
- References:
- Some queries example:
- Select all data from model objects
Person.objects.all()
- Select with criteria filter & order DESC
Person.objects.filter(id=23).order_by('-first_name')
- References:
- References:
- References:
- Command to install addons
# Python Mysqlpip install mysqlclient # Python PostgreSQLpip install psycopg2 # Django-Bootstrap-Formpip install django-bootstrap-form # Django Jinjapip install django-jinja # Pillow Lib (Image manipulation library for Python)pip install pillow # Bcrypt (Secure Password Hasher)pip install bcrypt
For Django best practice guide and reference, pleas view this page.
- Writing your first Django app, part 1
- Using a 3rd-party database backend
- Django-Bootstrap-Form Lib 1
- Django-Bootstrap-Form Lib 2
- Add User Profile To Django Admin
- 5 ways to make Django Admin safer
- Awsome Django Lib & Template
- PostgreSQL specific model fields
Coming soon...
