Skip to main content

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: django-admin.py startproject project-name [lets call it testproj]

testproj/
|-- manage.py
`-- testproj
|-- __init__.py
|-- settings.py
|-- urls.py
`-- wsgi.py

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

cd testproj
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]

 testapp
|-- admin.py
|-- __init__.py
|-- migrations
| `-- __init__.py
|-- models.py
|-- tests.py
`-- views.py


#To deactivate the virtual environment, execute the below command
deactivate

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
By default, INSTALLED_APPS contains the following apps, all of which come with Django:
$ python manage.py migrate
The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your testproj/settings.py file.


References:

  • https://docs.djangoproject.com/en/1.7/topics/install/
  • https://docs.djangoproject.com/en/1.7/intro/tutorial01/
  • https://www.youtube.com/watch?v=oT1A1KKf0SI
  • https://mikesdjangotutorials.co.uk/

Comments

Popular posts from this blog

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

My devstack installation was failing with an error reported by the GnuTLS module as shown below: $ git clone https://github.com/openstack/horizon.git /opt/stack/horizon --branch master Cloning into '/opt/stack/horizon'... remote: Counting objects: 154213, done. remote: Compressing objects: 100% (11/11), done. error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received. fatal: The remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed The following Git config changes fixed the issue for me. Am hoping it will be useful for someone out there: $ git config http.sslVerify false $ git config --global http.postBuffer 1048576000

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

Assumptions : You are using ML2 plugin configured to use Vlans If you try to create a network for a tenant and it fails with the following error: Error: Failed to create network "Test": 503-{u'NeutronError': {u'message': u'Unable to create the network. No tenant network is available for allocation.', u'type': u'NoNetworkAvailable', u'detail': u''}} The problem can be due to missing configuration in the below files: In /etc/neutron/plugins/ml2/ml2_conf.ini network_vlan_ranges =physnet1:1000:2999 (1000:2999 is the Vlan range allocation) In /etc/neutron/plugins/openvswitch/ovs_neutron_plugin.ini bridge_mappings = physnet1:br-eth1 (in OVS we map the physical network to the OVS bridge) Note You should have created a bridge br-eth1 manually and mapped it to a port ovs-vsctl add-br br-eth1 ovs-vsctl add-port br-eth1 eth1 Once configuration is done, restart the neutron ovs agent on the compute node(s): ...

QuickBite: Tap Vs Veth

Linux supports virtual networking via various artifacts such as: Soft Switches (Linux Bridge, OpenVSwitch) Virtual Network Adapters (tun, tap, veth and a few more) In this blog, we will look at the virtual network adapters tap and veth. From a practical view point, both seem to be having the same functionality and its a bit confusing as to where to use what. A quick definition of tap/veth is as follows: TAP A TAP is a simulated interface which exists only in the kernel and has no physical component associated with it. It can be viewed as a simple Point-to-Point or Ethernet device, which instead of receiving packets from a physical media, receives them from user space program and instead of sending packets via physical media writes them to the user space program. When a user space program (in our case the VM) gets attached to the tap interface it gets hold of a file descriptor, reading from which gives it the data being sent on the tap interface. Writing to the file descri...