Skip to content

Repository files navigation

Django Project Example

Imgur

Last Update

  • 13 Aug 2018, migrate to Django 2.1, adjust some code, add some README content.

Target :

  • 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.

0. Setup Python Virtual Environment

  • 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:

    Python Virtual Environment

  • Listing all current Virtual Env Library & Version

    pip freeze --local

    or

    pip list

1. Install and Create Django Project

  • 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

2. Project Setup

  • Open file <project-name>/settings.py
  • Search for DATABASE pythons 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
  • 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_ZONE variable, list of TZ available Here.
    TIME_ZONE='Asia/Jakarta'

3. Admin Page

  • Create an admin account, execute cmd: python manage.py createsuperuser

4. Model Fields

  • Common used MySQL database field to Django data type:
MySQL FieldDjango Model FieldParameters
VARCHARCharFieldmax_length, unique, null, blank, default, primary_key
VARCHAREmailFieldnull, blank, default
VARHCARURLFieldnull, blank, default
LONGTEXTTextFieldnull, blank, default
INTIntegerFieldnull, default
DECIMALDecimalFieldnull, default
TINYINT / BOOLEANBooleanFieldnull, default
DATEDateFieldnull, default
TIMETimeFieldnull, default
DATETIMEDateTimeFieldnull, default
LONGBLOBBinaryFieldnull, 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:

5. Making Model Queries

  • 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:

6. Forms Validator

7. View

8. Other Library and Addons

  • 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 

9. Django Best Practice, Standard & Convention

For Django best practice guide and reference, pleas view this page.

Other References

Python Tutorial

Coming soon...

About

Django Learning Example and Sample Project

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages