Django is a Python web framework that allows for rapid development of primarily database-driven web applications. Virtual Environments, or virtualenv, is a Python package that allows development within isolated Python environments. Installing or updating Python packages within one environment will have no impact on other environments. Combining Django and virtualenv provides a solid environment for both development and production deployment.
- CentOS 7 or RHEL 7
Many distributions include virtualenv and virtualenvwrapper as part of the default distribution repository. However, these version can become outdated and it is usually recommended installing from a repository containing the latest release.
One common repository added to CentOS and Red Hat Enterprise Linux (RHEL) servers is the Extra Packages for Enterprise Linux (EPEL) repository. The following command will add the EPEL repository to CentOS 7 or RHEL 7.
sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-2.noarch.rpm
Now virtualenv, and dependencies, can be installed using the YUM package manager.
sudo yum -y install python-virtualenv
The virtualenvwrapper package is a popular extension to virtualenv that increases usability; particularly when managing multiple virtual environments. While completely optional, it is recommend virtualenvwrapper be installed as this tutorial makes use of virtualenvwrapper features.
sudo yum -y install python-virtualenvwrapper
Configure Virtual Environment
The first step is to define the location of your virtual environments and load virtualenvwrapper.
export WORKON_HOME=~/.virtualenvs source /usr/bin/virtualenvwrapper.sh
The ~/.virtualenvs directory is where the above virtual environments will reside. This directory path was chosen because the EPEL python-virtualenvwrapper package will set this path globally for all users in /etc/profile.d/virtualenvwrapper.sh.
Your first virtual environment can now be created.
The shell prompt will change to reflect that the myapp virtual environment is loaded. You can leave the virtual environment at any time with the following command.
The prompt will change to reflect the virtual environment is no longer loaded. The following will return to, or “activate”, the virtual environment that was previously created.
Other useful virtualenvwrapper commands include:
- lsvirtualenv – List available virtual environments.
- cdvirtualenv – Change your current directory into the activated virtual environment.
- rmvirtualenv – Remove a virtual environment.
The Python pip package manager should be installed before using the virtual environment.
sudo yum -y install python-pip
Before you install Django, be sure to activate the new virtual environment if not already activated.
Now Django can be installed.
pip install django
The Django package files will be installed under ~/.virtualenvs/myapp.
Any further required Python packages should be installed in the same manner while the virtual environment is activated.
workon myapp pip install <package_name>
Create Django Project
You are ready to create a Django project.
django-admin.py startproject myproject
This will create a ./myproject directory containing several projects directories and files. Navigate into the project directory.
The ./myproject/settings.py file can be modified and a preferred database backend configured. Other settings can be modified as necessary. If no database backend is specified, then Django will use the default sqlite3 backend.
You can populate with database with some default Django tables.
python manage.py migrate
The project can now be verified by launching the Django web service.
python manage.py runserver
The service should now be accessible at http://localhost:8000/. This can be verified with curl.
The default IP address and port can easily be changed on launch. Simply add the following parameter to bind to all available IP addresses and port 8080.
python manage.py runserver 0.0.0.0:8080
Note: The built-in Django web server is NOT meant to be used in a production environment. It is a tool for development only. Once the application is ready to be deployed into production, consider using a web server such as Apache or Nginx with uWSGI.
Create Django Application
Once the project is created and operation, it is time to create an application. An application can be created in the same directory as manage.py.
python manage.py startapp myapp
This will create a ./myapp directory with base files ready for your code.