Archive for April, 2017

Create Django App and Data Model

April 23, 2017 1 comment

Creating Django App

To manage a project, django divide the project into several small django apps. Django app is a group of related functionality used to complete or maintain one aspect of a site.

The following command will create django app called myapp

$ python startapp myapp

To make django load the app, we need to add app name in django setting:

$ nano mysite/

    # project app

Creating Data Model

We often need to store our data into database. In django, we need to define our data model first, then make django to propagate data model creation/changes to database schema by using django commands. In this example, we will create data model inside of django app that we created before.

  1. Create data model
    $ nano myapp/
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    from django.db import models
    class MyModel(models.Model):
        foo = models.IntegerField(default=0)
        bar = models.TextField(blank=True, null=True)
        class Meta:
            db_table = 'my_model_tbl'
  2. Make django migration
    $ python makemigrations myapp
  3. Apply changes to database schema
    $ python migrate myapp


Categories: Django, Framework

Install Django with Postgresql

April 22, 2017 Leave a comment

This post will guide you quickly step-by-step installing django and Postgresql on Linux Mint.

  1. Install Postgresql Server
    $ sudo apt-get install postgresql
  2. Switch account to postgres and create db user for django project (create non superuser that can create db)
    $ sudo -i -u postgres
    $ createuser djangouser -P --interactive
  3. Connect to postgresql server as djangouser and create db for django project
    $ psql -u djangouser -h localhost -d postgres -W
    $ create database django_project_db
  4. Create Django Environment and install postgres db adapter for python
    $ mkvirtualenv django-project
    $ pip install django psycopg2
  5. Create Django Project
    $ django-admin startproject mysite
  6. Change django project db settings
    $ cd mysite
    $ nano mysite/
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'django_project_db',
            'USER': 'djangouser',
            'PASSWORD': 'djangouser',
            'HOST': 'localhost',
            'POST': '5432'
  7. Initialize DB data for django project
    $ python migrate
  8. Test by running the server
    $ python runserver


Categories: Django, Linux

Setup Python Virtual Environment

April 17, 2017 Leave a comment

When working on multiple python projects, perhaps we need to isolate package dependencies that needed for each project, so that dependency changes on one project does not make other project stop working.

Here’s example how to setup python virtual environment on linux mint.

$ sudo apt-get install python-pip
$ pip install setuptools virtualenv virtualenvwrapper

Add following code in ~/.profile

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/

Execute following command

$ source ~/.profile

Done! It’s time to test.

  1. Create Virtual Environment for project 1: eg env1 (skip this if env1 already created)
    $ mkvirtualenv env1
  2. Activate virtual anvironment (eg: env1)
    $ workon env1
  3. Install package dependencies to current environment (eg: django)
    (env1)$ pip install django
  4. To get info about installed package:
    (env1)$ pip freeze
  5. Deactivate to exit from current virtual environment
    (env1)$ deactivate
  6. Get available virtual environments
    $ workon
  7. Remove virtual environment
    $ rmvirtualenv env1


Categories: Linux, Python