SimplerCloud Pte Ltd

×
×

Where is Django installed on my servelet running the Ubuntu 16.04.4 (64-bit) + Django 2.0.4 on Python 3.5.2 OS template?

Back

Django is installed on a virtual environment (created using virtualenv) within your servelet so that your Django development environment will be isolated from the servelet's global environment. You may use the existing virtualenv which is already provided on the template and comes with Django version 2.0.4, or you may create a new virtualenv and install your own preferred version of Django if needed.

The virtual environment is called "django-env" and it's located under /root/django-apps folder. Note that by default this "django-apps" folder is located on the root-disk, you should move this to the data-disk (e.g. /data/django-apps folder) so that your future codes will not take up the root-disk space.

To activate the existing virtual environment, go to the django-apps folder:

cd /root/django-apps  (or cd /data/django-apps if you have moved this folder to the data-disk)
. django-env/bin/activate

You will then enter the virtual environment, we can see from the prompt, which will be similar to below:

(django-env) root@test-ubuntu1604-django2-20180423:~/django-apps#

You will see that Django is already installed within the virtualenv by typing command: django-admin --version

(django-env) root@test-ubuntu1604-django2-20180423:~/django-apps# django-admin --version
2.0.4

From the above result, we can see that Django version 2.0.4 is already installed and available within the virtualenv. You may then continue to create a Django project / site using the django-admin command. E.g.:

django-admin startproject sctest

The above command will create a new Django project/site called "sctest". A new project folder (in this case: "sctest") will be created and you can just change to the directory and start coding.

You can also start the project server and view the site using the "runserver" command. First, you will need to add the public IP address of your servelet to the ALLOWED_HOSTS settings under the settings.py file under PROJECTNAME/PROJECTNAME folder. E.g.

cd sctest/sctest
nano settings.py (or vi settings.py)

Add the servelet's public IP address under the ALLOWED_HOSTS line setting, e.g.:

ALLOWED_HOSTS = ['1.2.3.4']

Note that 1.2.3.4 is just an example, you will need to change it to the actual IP of your servelet. Save the file once done. Go back up one folder and run below command:

cd ..
python manage.py runserver 1.2.3.4:8000

Again, change the above IP address with the correct IP address of your servelet. E.g.

(django-env) root@test-ubuntu1604-django2-20180423:~/django-apps/sctest# python manage.py runserver 1.2.3.4:8000
Performing system checks...

System check identified no issues (0 silenced).

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 23, 2018 - 07:23:50
Django version 2.0.4, using settings 'sctest.settings'
Starting development server at http://1.2.3.4:8000/
Quit the server with CONTROL-C.

Your Django project site should be up and running, and you should be able to access the Django default web site at below URL:

http://1.2.3.4:8000/

That's it, you have run your first Django project site. Use Ctrl-C to stop the server, and type "deactivate" to leave the virtualenv and go back to the servelet's original shell.

Quit the server with CONTROL-C.
^C(django-env) root@test-ubuntu1604-django2-20180423:~/django-apps/sctest# deactivate
root@test-ubuntu1604-django2-20180423:~/django-apps/sctest#

To create a new virtual environment, go to the django-apps folder and use below command:

cd /root/django-apps (or cd /data/django-apps if you have moved the folder to the data-disk)
virtualenv VIRTUALENVNAME

e.g.

root@test-ubuntu1604-django2-20180423:~/django-apps# virtualenv django-env-2
Using base prefix '/usr'
New python executable in /root/django-apps/django-env-2/bin/python3
Also creating executable in /root/django-apps/django-env-2/bin/python
Installing setuptools, pip, wheel...done.

To go to the newly created virtualenv, type:

. VIRTUALENVNAME/bin/activate

e.g.

root@test-ubuntu1604-django2-20180423:~/django-apps# . django-env-2/bin/activate
(django-env-2) root@test-ubuntu1604-django2-20180423:~/django-apps#

To install Django within the newly created virtualenv, use pip:

(django-env-2) root@test-ubuntu1604-django2-20180423:~/django-apps# pip install django
Collecting django
  Using cached https://files.pythonhosted.org/packages/89/f9/94c20658f0cdecc2b6607811e2c0bb042408a51f589e5ad0cb0eac3236a1/Django-2.0.4-py3-none-any.whl
Collecting pytz (from django)
  Using cached https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-2.0.4 pytz-2018.4
(django-env-2) root@test-ubuntu1604-django2-20180423:~/django-apps#

To verify:

(django-env-2) root@test-ubuntu1604-django2-20180423:~/django-apps# django-admin --version
2.0.4

Was this article helpful?
Dislike0 Like0
Views: 655