How To Install Virtualenv & Django Framework

https://www.digitalocean.com/community/tutorials/how-to-install-the-django-web-framework-on-ubuntu-16-04

http://tutorial.djangogirls.org/en/django_start_project/

The Python virtualenv package allows you to create self-contained environments for various projects. Using this technology, you can install Django in a project directory without affecting the greater system. This allows you to provide per-project customizations and packages easily. Virtual environments add some slight mental and process overhead in comparison to globally accessible installation, but provide the most flexibility.

Install virtualenv

$ sudo pip install virtualenv

Now, whenever you start a new project, you can create a virtual environment for it. Start by creating and moving into a new project directory:

$ mkdir django5.0

$ cd django5.0/

Now, create a virtual environment within the project directory by typing:

$ virtualenv djangovenv5.0

This will install a standalone version of Python, as well as pip, into an isolated directory structure within your project directory. Choose any descriptive name that you want for the virtualenv. A directory will be created with the name you select, which will hold the file hierarchy where your packages will be installed.

To install packages into the isolated environment, you must activate it by typing:

$ source djangovenv5.0/bin/activate

Your prompt should change to reflect that you are now in your virtual environment. It will look something like:

(djangovenv5.0)username@hostname:~/django5.0$

Install Django

In your new environment, you can use pip to install Django. Regardless of whether you are using version 2 or 3 of Python, it should be called just pip when you are in your virtual environment. Also note that you do not need to use sudo since you are installing locally:

$ pip install django

You can verify the installation by typing:

django-admin --version

Output:

1.11.3

How to deactivate virtual environment

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

(djangovenv5.0)$ deactivate

Your prompt should revert to the conventional display. When you wish to work on your project again, you should re-activate your virtual environment by moving back into your project directory and activating:

$ cd django5.0/
$ source djangovenv5.0/bin/activate

Start a project

If you were already in a project directory that you created for use with the virtualenv command, you can tell Django to place the management script and inner directory into the current directory without the extra layer by typing this (notice the ending dot). ‘projectname' is any name you like:

$ django-admin startproject projectname .

$ cd projectname

This will create a directory called ‘projectname' within your current directory. Within this, a management script will be created and another directory called projectname will be created with the actual code.

Then, to bootstrap the database:

$ sudo python manage.py migrate

Create an administrative user by typing:

$ python manage.py createsuperuser

You will be prompted for a username (leave blank to use ‘vagrant'), an email address, and a password for the user.

To start up the development server. The command below assumes vagrant is set to forward_port ‘8000' in Vagrantfile. For example, if set to ‘8888' (or '80', etc), adjust the code to reflect this below. Alternatively – inside Virtualbox, select the virtual machine -> Settings -> Network -> Advanced -> Port Forwarding -> add a port forwarding rule to allow Host/Guest port 8000. For example, Name: ‘tcp8000' Host Port: ‘8000' Guest Port: ‘8000'.

$ python manage.py runserver 0.0.0.0:8000

Visit your server's IP address followed by :8000 in your web browser:

127.0.0.1:8000

and http://127.0.0.1:8000/admin


The code below has not been re-tested

Using Python 3 in virtualenv

https://www.youtube.com/watch?v=FNQxxpM1yOs&index=1&list=PLQVvvaa0QuDeA05ZouE4OzDYLHY-XH-Nd

Same as above:

$ sudo apt-get install python-pip

$ sudo pip install virtualenv

$ mkdir virtual-env

$ cd virtual-env/

Here we have a slight change:

$ virtualenv -p python3 django-virtual

The we continue as before:

$ cd django-virtual/

$ source bin/activate

$ pip install django

$ django-admin startproject mysite

$ cd mysite

4 sudo python manage.py migrate

$ python manage.py runserver 0.0.0.0:8000

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.