Just a few commands will get you started with a site using Django on our optimised, pre-installed image of Ubuntu 13.04. If you want to use a different Linux distribution, most of this tutorial will still be correct. You will have to translate all of the apt-get statements and package names to your distribution of choice. Configuration file paths and layouts may also differ. If you would like more information about how to set up a Linux VM using our system, please visit our other tutorials.
Step 1: Install Pip
First, fire up a fresh terminal and ensure your package lists are up-to-date:
sudo apt-get update
Now install Pip. It’s a package-manager for python.
sudo apt-get install python-pip
Now install Python. The “-O” option makes sure that the filename is the same as its source file.
curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
Now we make a new virtual environment. This contains our project so that every subsequent update or install occurs only within this environment. This is just good practise, but is also extremely useful if you have multiple projects running on the same server. As you can see, I’ve named this environment “myenvironment”.
python virtualenv.py myenvironment
Now we can activate this environment:
Step 2: Install Django
Having installed Pip, it’s very easy to get django running. Its use is very similar to other package managers:
pip install django
If you’ve already installed Django, you can upgrade it using this command:
pip install --upgrade Django
Step 3: Set up a project
From here on, this tutorial is based upon the far more extensive official one found at https://docs.djangoproject.com/en/dev/intro/tutorial01/, so if you’re having trouble I strongly suggest checking out their documentation as it has support for all the vastly differing versions of django. First, head to a file where you want to create your project. I’m going to make a new directory inside /home/ called “mysite”, so I type:
It’s best not to keep your code in the document root (like /var/www), that’s why I’ve put it here. Now I need to head there, so I type:
I’m happy to store my code here, so I use this command to create a project template:
django-admin.py startproject mysite
The template creates a standard layout. You can see what it looks like by typing:
It should look like this. The first line is the top level, with one further directory listed below, predictably called “mysite”:
.: manage.py mysite ./mysite: __init__.py settings.py urls.py wsgi.py
Step 3: Check it works and make it live!
Django is a great tool for making templates, so it comes with almost everything needed to get you up and running as quickly as possible. We can run the test page now by going to the main project directory, that’s the place where you can find the manage.py file. We need to use that file to start the server, like so:
python manage.py runserver
If everything went according to plan, you should see this output (It’s always good to read “0 errors found”):
Performing system checks... 0 errors found February 17, 2014 - 15:50:53 Django version 1.7, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
The Django development server is now running. This is a lightweight web server included with Django so that you can easily check how things look while you’re in development. You’re only running this server locally now, so you can visit it locally, or specify the port. To listen on all public IPs (which will make your site live on the specified port), use:
python manage.py runserver 0.0.0.0:8000
Step 4: Bask in the glory of your own awesomeness
Now if you head to the ElasticHosts control panel and find your server’s IP address, you should be able to see your site template live on that port by heading to the URL in a browser from any computer: http://YOURIPADDRESS:8000 Well done!