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]
- lib : Contains the python libraries
#This step activates the virtual environment
Step 4: source bin/activate
Step 5: easy_install Django
#This step creates a test django project
Step 6: django-admin.py startproject project-name [lets call it 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.
- manage.py: 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/__init__.py: An empty file that tells Python that this directory should be considered a Python package.
- testproj/settings.py: Settings/configuration for this Django project.
- testproj/urls.py: The URL declarations for this Django project
- testproj/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
#This step starts the django server
Step 7: ./manage.py runserver port-number [default 8000]
#This step creates the folders and templates files required to create a django app.
Step 8: ./manage.py startapp app-name [lets call it testapp]
| `-- __init__.py
#To deactivate the virtual environment, execute the below command
Initial Configuration (Database, TimeZone, App setup)
Use testproj/settings.py 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
- django.contrib.admin – The admin site.
- django.contrib.auth – An authentication system.
- django.contrib.contenttypes – A framework for content types.
- django.contrib.sessions – A session framework.
- django.contrib.messages – A messaging framework.
- django.contrib.staticfiles – A framework for managing static files.
$ python manage.py migrate