Preliminaries
Welcome! We’re going to show you how to install the Django Web Framework on your Ubuntu 14.04 Stack. Django is a full-fledged web framework that follows the model-view-controller pattern for web development. Django focuses on code modularization for reusability and rapid development — in line with the DRY (don’t repeat yourself) philosophy. It is popular with developers that have a good knowledge of Python, since everything from configuration to adjusting data models is done in python. We’re going to assume that you have either Python 2.7 or Python 3 already installed. In addition, you should have configured your Stack with a non-root user with sudo privilages as we did in the “Getting Started” tutorial. Without further ado, let’s begin.
Installing Django
There are a number of ways we could install Django. If you just want a simple installation of Django and do not care about bleeding-edge features, installing the package from the official Ubuntu repositories is sufficient. If you want the latest stable release of Django along with the flexibility to install addons, follow the pip installation section.
Installing Django from Official Ubuntu Repositories
We’re first going to update our package repositories by running apt-get update
sudo apt-get update
Now we can install Django by running the following command:
sudo apt-get python-django
To see if the installation proceeded correctly, enter the following into your terminal
python -c "import django; print(django.get_version())"
If you see a version number, you’re ready to create a sample Django project. Type the following command replacing sample_project appropriately
django-admin startproject sample_project
This creates a skeleton Django project that you will be able to configure. The directory strucuture is arranged as follows
sample_project/ manage.py sample_project init__.py settings.py urls.py wsgi.py
manage.py is a command line python script with which you will interact to run some common actions such as starting up the web server or performing database migrations. As you can see, a sample_project folder has also been created. This is the root python package for the Django project. If you wish to create any other packages from which you will import objects, create a folder here matching the name of the package.
Initializing the Database
Run the following command
python manage.py migrate
This command will properly initialize the empty database for the project. By default, Django uses sqlite3 which is fine for close to 100,000 records. For anything more, you should consider MariaDB or PostgreSQL depending on your needs. If you do decide to use another database, you will first need to install appropriate python database bindings and then configure the DATABASES variable in sample_project/settings.py.
Finally, we can run our server by executing the following command in the root folder of your project,
python manage.py runserver 0.0.0.0:8000 System check identified no issues (0 silenced). June 01, 2015 - 12:33:55 Django version 1.8.2, using settings 'mytest.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
This starts your server which begins listening on port 8000 for any incoming requests and routes them appropriately. You can visit the default Django page by pointing your browser at http://your_stacks_ip_address:8000. Congratulations, you are now ready to start developing using Django and Python. If you want to get started with making your first Django app, check out this tutorial released by Django. Otherwise, check out the detailed documentation.
Installing Django Using Pip
We’re going to be using pip, the Python package manager. If you’ve used tools such as bundler and npm, you’re aware of the value that package managers bring to development. Let’s begin.
First, update your Stack’s package repositories.
sudo apt-get update
Now we’ll install pip. Run the following
sudo apt-get install python-pip
or
sudo apt-get install python3-pip
depending on which version of Python you wish to run. Once this has finished installing, you can simply run
sudo pip install Django
or
sudo pip3 install Django
depending on which version of Python you installed earlier. One this completes, run the following command
python -c "import django; print(django.get_version())"
to see if your installation proceeded correctly.
Congratulations, you’re ready to get started with Django and pip! Type the following command replacing sample_project with an appropriate name for your own Django project.
django-admin startproject sample_project
This creates a skeleton Django project that you will be able to configure. The directory strucuture is arranged as follows:
sample_project/ manage.py sample_project init__.py settings.py urls.py wsgi.py
manage.py is a command line python script with which you will interact to run some common actions such as starting up the web server or performing database migrations. As you can see, a sample_project folder has also been created. This is the root python package for the Django project. If you wish to create any other packages from which you will import objects, create a folder here matching the name of the package.
manage.py is a command line python script with which you will interact to run some common actions such as starting up the web server or performing database migrations. As you can see, a sample_project folder has also been created. This is the root python package for the Django project. If you wish to create any other packages from which you will import objects, create a folder here matching the name of the package.
Initializing the Database
Run the following command:
python manage.py migrate
This command will properly initialize the empty database for the project. By default, Django uses sqlite3 which is fine for close to 100,000 records. For anything more, you should consider MariaDB or PostgreSQL depending on your needs. If you do decide to use another database, you will first need to install appropriate python database bindings and then configure the DATABASES variable in sample_project/settings.py.
Finally, we can run our server by executing the following command in the root folder of your project,
python manage.py runserver 0.0.0.0:8000 System check identified no issues (0 silenced). June 01, 2015 - 12:33:55 Django version 1.8.2, using settings 'mytest.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
This starts your server which begins listening on port 8000 for any incoming requests and routes them appropriately. You can visit the default Django page by pointing your browser at http://your_stacks_ip_address:8000. Congratulations, you are now ready to start developing using Django and Python. If you want to get started with making your first Django app, check out this tutorial released by Django. Otherwise, check out the detailed documentation.
Final Words
Congratulations! You’ve successfully installed a working Django environment on your Stack. You’re ready to dive into the exhilarating world of web development using Python. Check out our Community Section for more tutorials on how to perform various server administration and development/operations tasks. From all of us at Stack Harbor, ahoy!