[box type=”warning”]Since I created this post things broke. Namely checking out the latest versions of the Chef cookbooks introduced a whole rat’s nest of dependencies. I spent a day trying to figure them out, but finally decided to just do provisioning via a shell script.
The repository has been updated so the latest version uses the new shell script. If you want the Chef version, it was tagged “v1WChef”.
[/box]
In a previous post I talked about how to do WordPress Development right by using Vagrant to create a virtual machine to do your development in.
I’ve been wanting to improve my Django skills and have a project I want to use it for, so I plan to use the same principle and do my Django coding in a VM using Vagrant.
Figuring out how to do this quickly, easily and reliably was more complicated than I expected. I owe a great debt to Smiley Creatives’ article “SETUP A DJANGO VM WITH VAGRANT, VIRTUALBOX, AND CHEF” and a comment by juanje on how to create a Chef cookbook to install Django.
My goal was to create something where I could run a script and have a whole new development environment for Django set up.
The Vagrant Django Script
If you don’t care how it works, but just want to use it, then do these steps.
1. Clone the repository to your machine.
I’ve created a git repository on bitbucket.org with all the files you need. Just execute this command.
This will create a folder on your machine. This folder is not where you are going to run Vagrant from. Rather it is a source folder for creating another folder to run Vagrant from and do your Django development.
2. Create a Target Folder
Make a directory for your development. Note the path to that directory.For our purposes, we’ll assume you want this directory to be next to the directory just created by the git clone command in step 1.
3. Run the Create Django VM Script
In the folder that was created by git you will find a script ‘create_django_vm.py’. Execute this script with the path to the directory you created in step 2.
python create_django_vm.py ../VMTEST/
If you look in the new directory you’ll see something like this:
drwxr-xr-x 12 rondavis 408 Feb 5 14:39 cookbooks
drwxr-xr-x 2 rondavis 68 Feb 5 14:39 django_shared
You have a new Vagrantfile, a folder full of Chef cookbooks, and a django_shared folder that will be shared with the VM.
4. Move To Your New Directory and Vagrant Up
That’s it, you are ready to go.
vagrant up
That’s it. There will be a bunch of Vagrant launching text, but when it’s finished you can:
You’ll find a directory in your home directory named “django_shared”. This is the same as the folder “django_shared” in the directory you just vagrant sshed from.
From here you can do all your django commands on your VM. Edit the files on your host OS, and view the app via your browser.
Running Django On Your VM
Let’s make sure our development environment is able to run Django by duplicating the first steps of the Django Tutorial.
Once you are logged in:
vagrant@lucid64:~$ django-admin.py startproject mysite
vagrant@lucid64:~$ cd mysite
vagrant@lucid64:~$ python manage.py runserver 0.0.0.0:8000
First we move into our shared directory, then we create a new Django project with the django-admin.py script. Then move into the newly created app folder and run the server.
Now go back to your browser and point it to http://localhost:8000/ and you should get the standard Django welcome.
A special note about the runserver command on your VM. You can’t just use the default ‘manage.py runserver’ because the VM won’t let that be shared. Instead you must tell it to run the server on 0.0.0.0:8000 to let it be seen outside the VM
I think the code that makes all this happen is well documented, so I won’t go over it all here. Let me know if you use this script and how you like it.
You do not have access to this repository.
Sorry about that Andy. I’ve made it public now. Please read the warning at the top of the post for changes since then.
Hello Ron.
Running “python create_django_vm.py ../VMTEST/” always throws an invalid syntax error for line 44 in create_django_vm.py for me.
Maybe you have an idea how to get around this error…
Michael:
Sorry I didn’t see this when it came in. I don’t have the problem and looking at the script that’s a very simple print statement on line 44. The only thing I can thing might be an issue, is if somehow there is a tab instead of spaces there. This happens to me sometimes if my editor decides not to convert my space to tabs. But I looked at the file and it is spaces.
Let me know if you figure it out.
Ron
Your note about 0.0.0.0:8000 saved me from days of frustration. Thanks!