Django is easily the most popular web framework written in Python. It strikes a delicate balance between feature completeness and efficiency, including powerful features like automatic migration generation and a full-featured admin interface. Setting up a Django development environment in Ubuntu is fairly easy, and can be done in only a few steps.
It’s best to use a Python virtual environment when developing in a complex framework like Django, especially if you intend to work on multiple projects at once. It’s also probably a good idea to make sure that you have both versions of Python up to date.
$ sudo apt-get install virtualenv python python3
Creating The Environment
Virtualenv allows a project to be sandboxed away from the system’s Python install. This allows a regular user to install Python packages and projects to use specific versions that may be different than the system versions. Creating a virtual environment with Virtualenv can be done with one command.
$ virtualenv -p python3 project-directory
specifies the version of Python to use in the environment. You can get more specific with something like
, but just running it like in the example above will choose the latest release of that chosen Python version currently installed on the system. Virtualenv will not pull in new versions of Python that aren’t installed on the system.
Activating The Environment
To use Virtualenv,
into the directory that you created, then activate the environment.
$ cd project-directory
$ source bin/activate
You will see the name of the directory appear at the beginning of your command prompt in parentheses. This indicated that you are using the virtual environment.
The virtual environment includes the Python package manager, Pip, to allow you to easily install and manager Python packages. Django is one of those packages, as are many of its add-ons and plug-ins. Pip can be used similarly to a distribution’s package manager, so use it to install Django, and it will pull in all of Django’s dependencies as well.
$ pip install django
This will pull in the newest available version of Django. If you want a specific version, it can be specified to Pip as well.
$ pip install django==1.9
Setting Up A Django Project
Now, you can start up a Django project and get working. The Django package that Pip installed provides a command to create the project.
$ django-admin startproject project-name
into your project.
$ cd project-name
set up the project as well as the basic files needed to get started. If you look in the current directory, you will see them. The most important one for getting set up is
. It is the central management script for the entire project. It is also the the script used to create database migrations and migrate them. So, use it to create any migrations, migrate, then set up your superuser account for the admin interface.
$ python manage.py makemigrations
$ python manage.py migrate
$ python manage.py createsuperuser
That’s it. Now, your Django project is set up and ready to go for development! When you’re done, you can leave your virtual environment with another simple command.