2017-01-27



About Django

Django is a great web application development framework, especially for the perfectionist with deadlines built into python programming language. It provides a very good structure and easy methods that help to do the heavy lifting when writing web applications.

If you have never used Django before and are looking for an installation guide on your Ubuntu machine then you are in the right place. In this article we will teach you how to set up a very simple Django development environment, step by step.

Some Tools We Need

There are many different ways to install the Django framework in Ubuntu. In order to stay updated with the latest python development industry standards we need to install a few tools before getting Django up and running it on our machine.

The first tool is called pip which is a very practical python package management system widely used by python developers all over the world. I mainly use pip to install packages for my python projects, generate dependencies of a project and also uninstall different python packages from my machine when they are no longer required.

Before installing pip we highly recommend updating the package index on your machine with the help of the command shown below.

The following command can be used to install the pip python package manager inside Ubuntu:

Now, you can easily install python packages on your Ubuntu machine by following the syntax shown here:

But we do not recommend installing packages yet! Python developers use another tool during the development of their python projects…

This tool is called virtualenv. It is really useful as it helps to manage dependency conflicts between different python projects on the same machine. Essentially, this tool helps to create isolated virtual environments where you can develop without any worries about package conflicts.

To install virtualenv on Ubuntu  the following command can be used:

Create A Virtual Environment For Your Project

Virtualenv can be easily used to create isolated virtual environments for your python projects.

For example, to create a virtual environment under the name of venv1 the following command can be used:

In order to work on an isolated environment it needs to be activated. The following command will make that happen:

But working with virtualenv is a bit annoying as there are many different commands you need to remember and type on your terminal emulator.

The best solution is to install and use virtualenvwrapper which makes working with python virtual environments very easy. Once you have finished installing and configuring virtualenvwrapper working on a virtual environment is as easy as typing the following command:

Now that pip and virtualenv tools are available on your machine it is time to install and configure virtualenvwrapper. To install virtualenvwrapper use pip, as shown below:

There are a few steps you need to follow in order to do this properly. On your command prompt run the following command:

All virtual environments you create will be available inside the directory ~/.virtualenvs.

If you want to keep your virtual environments inside another directory then use the following commands (as shown in the official documentation of the virtualenvwrapper):

I like to keep my virtual environments inside ~/.virtualenvs. My projects are inside ~/projects.

To create a virtual environment just use the command mkvirtualenv as shown below:

The following output is displayed on my terminal when executing the above command.

To work on the virtual environment linuxtuts use the following command:

Once the virtual environment has been activated your command prompt will change. Mine looks like this:

As you can see from the output shown above, linuxtuts is the name of the virtual environment we created.

Make the changes permanent

In order for the commands offered by virtualenvwrapper to work every time you open the terminal you will need to make some changes in the .bashrc file.

The .bashrc file is used to set up environment variables, function aliases and lots of other information you will want to have when opening a new terminal window. Read more on .bashrc.

Open the .bashrc file with your text editor, and then copy/paste the following into it and save the file.

Install Django

Inside the virtual environment use the command ‘which python’ to see the python executable you are using.

The above command produces the following output on my machine. Depending on the name of your directory for the virtual environments you will get a different output, but structured in a very similar way.

The above output is telling us that the python executeable being used is inside the virtual environment, which in this case is linuxtuts.

Deactivate the virtual environment with the command deactivate.

Remove the virtual environment linuxtuts with the help of the following command:

The reason we decided to remove linuxtuts is that we did not choose the version of python we wanted to use inside the virtual environment we created.

It can be done with the following command:

In a project I am working on we use python3.2. To run the project I create a special environment for it:

The above command produces the following output:

The command prompt will look similar to mine.

You can use any python version required by your project. The installation of Django is very easy. Just run the following command:

The above command produces the following output:

To install a different version of Django, specify the version you would like to use, as shown here:

It is up to you which version of python and Django you want to use for your projects.

Set up your first simple project in Django

After you have finished the installation of Django in a virtual environment, you probably want to start your first project.

Fortunately for us, Django offers management tools for your projects. The django-admin.py tool can be used to start the project.

Create a fresh virtual environment for your new project.

Note: Depending on the version of python you want to use you need to change the path listed above.

Install a new copy of Django inside your virtual environment.

Use the following command to start a fresh Django project.

Then cd to your project directory and list the files inside your project. The directory structure will be similar to the one shown below.

The manage.py is another tool you have to work on your Django projects. Each Django project is composed of apps.

You can create a new app with the following command:

If you do an ls now, the directory structure should look like the one shown below:

It is always a good idea to generate the requirements.txt file for every project you write in Django so that when you show it to your friends or want to run the project in another machine you can easily install the needed packages to run the project.

Create a new file called requirements.txt on the top level directory of your project and append the packages there using the following syntax:

As you can see from the above command, you append the package name then add its version. This is very important.

If you do an ls now, inside your project you should get the following:

Install the requirements for the project on a new machine by using the following command:

Are you asking yourself how to run the Django project? Just run it using the following command which starts the built-in Django development server:

And then visit http://127.0.0.1:8000/. You will get a message from Django congratulating you on running your first Django project!

Conclusion

Knowledge on tools such as virtualenv and virtualenvwrapper is a must for a python developer. In this article I showed you how to install Django on Ubuntu and also how to set up a very basic Django development environment.

I will publish another tutorial on Django where I will explain in detail how to set up a professional Django development environment in linux.

The post How to install Django on Ubuntu (the right way) for novice users appeared first on Unixmen.

Show more