Setting up Python Django dev environment

Django is a python web development framework that helps in rapid application development. Once you are comfortable with the basics, developing UI applications with Django framework is a breeze and helps the developers to focus on the problem at hand rather than on UI nitty gritty.

An understanding of this framework is a must in order to develop applications for Openstack Horizon module.

In this article, I will list out the steps that need to be carried out to get a Django setup ready for the initial learning phase. At the end of this article, I have included all the links that I have followed when I initially learnt this framework.

The cleanest way to setup Django on your system is to use a virtual environment, which creates a sandbox for your setup and isolates the changes to the system within that container. For more information on Virtual environment you can refer to virtualenv

There are multiple ways in which Django can be installed. This article uses the combination of easy_install and virtualenv to install Django. For alternate ways, you can refer to the official Django documentation.

Step 1: sudo apt-get install python-setuptools

Step 2: sudo easy_install virtualenv

#This step creates a sandboxed virtual environment
Step 3: virtualenv env-name [lets call it testenv]

testenv has the following dir structure:
- bin : Contains the executables, mainly python [django binaries get added later]
- include
- lib : Contains the python libraries
- local

#This step activates the virtual environment
cd testenv
Step 4: source bin/activate

Step 5: easy_install Django

#This step creates a test django project
Step 6: startproject project-name [lets call it testproj]

`-- testproj

  • The outer testproj/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • A command-line utility that lets you interact with this Django project in various ways.
  • The inner testproj/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. testproj.urls).
  • testproj/ An empty file that tells Python that this directory should be considered a Python package.
  • testproj/ Settings/configuration for this Django project.
  • testproj/ The URL declarations for this Django project
  • testproj/ An entry-point for WSGI-compatible web servers to serve your project.

#This step starts the django server

cd testproj
Step 7: ./ runserver port-number [default 8000]

#This step creates the folders and templates files required to create a django app.
Step 8: ./ startapp app-name [lets call it testapp]

|-- migrations
| `--

#To deactivate the virtual environment, execute the below command

Initial Configuration (Database, TimeZone, App setup)

Use testproj/ file to configure the application. Some options available are:
  • DEBUG = True
  • INSTALLED_APPS -- Add your app to this list
  • DATABASES -- by default sqlite db is used as it comes bundled with python. You can change it to the db of your choice.
  • TIME_ZONE = 'UTC' -- Update this to your time zone
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
$ python migrate
The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your testproj/ file.




Popular posts from this blog

Solved: Fix for Git clone failure due to GnuTLS recv error (-9)

Openstack : Fixing Failed to create network. No tenant network is available for allocation issue.

QuickBite: Tap Vs Veth