VIDEO: Virtual Django Environment with Vagrant

Set up a virtual Django development environment for Python applications. Create this with Vagrant, Ubuntu, and Virtualbox.

Inside the command line/Terminal:

Start a django app

              django-admin startproject YOURPROJECTTITLEHERE

Then change directory into your project file

              cd YOURPROJECTTITLEHERE

Initialize a vagrantfile

vagrant init techbytes/djangoubuntu

Configure our Vagrantfile

Forwarded port

 Uncomment out the forwarded port code by removing the # from config.vm.network “forwarded_port”, guest: 80, host: 8080

Private network

Uncomment out the private network and change the IP address to “55.55.55.5”

Because the django server is configured to only work on your computer or the localhost

In this case we are a guest connecting to virtual host machine

So we need to set up a private network — in this case we will use 55.55.55.5

Then we need to add this to our settings.py file as an Allowed Host

Stand up our Vagrant box

vagrant up

Change directory into our /vagrant folder

Perform any migrations that may be necessary

python3 manage.py migrate

Run the server

python3 manage.py runserver 0.0.0.0:8000

Go to your browser and type in 55.55.55.5:8000 and view your success message

If this is your first time building a Django app add /admin to your url and get a little more impressive portion of the built in admin functionality of django

To review all this you will need to create a superuser and password — and I will leave that to you

Now what would happen if we did not add the ip address to the Allowed Hosts — let’s see —

Go back to the settings.py and remove the string, save and then reload your browser

The python app still comes up, but with an invalid host message — luckily the message is rather verbose and tells you what you need to do — add ‘55.55.55.5’ to the Allowed Hosts

So let’s pop that back in and see it working again

Now once you are done working in your virtual environment you will want o bring down your virtual machine — exit out of the vagrant ssh, and vagrant destroy, suspend, or halt your machine — I am going to vagrant destroy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: