[Solved]-How did you setup your Django dev environment?

2👍

I have a public repo on GitHub available here:

https://github.com/FlipperPA/djangovagrant

Instructions from the README.md:

Django / Python / MySQL

This is a Vagrant project for Django development.

This does not yet support berkshelf or librarian; all necessary repos are included in ‘cookbooks’.

Prerequisites, all platforms:

Virtualbox https://www.virtualbox.org/wiki/Downloads
Vagrant http://downloads.vagrantup.com/

Pre-requisites, Windows only:

git-bash
ruby rvm

Fairly easy to get it running:

vagrant up
vagrant ssh djangovm

** (Note: You are now in the Virtualbox VM as superuser vagrant)

sudo apt-get install python-pip

** (Note: PIP is a Python package manager)

sudo apt-get install python-mysqldb
sudo pip install django

Starting a Django project:

django-admin.py startproject django_project
cd django_project
python manage.py runserver [::]:8000

The VM is configured to use port forwarding. If everything went right, you should be able to access the running server through the browser on your computer running the virtual machine at this url:

http://localhost:8001/

New to Django? Next steps? I highly recommend: http://www.tangowithdjango.com/
For more advanced topics, check out Two Scoops of Django: http://twoscoopspress.org/

1👍

there are a few django Apps that I’ve seen to manage this but I always prefer the following in my settings.py as the number of different configs are usually minimal

SITE_TYPE = environ.get( 'SITE_TYPE', 'DEV' )

if SITE_TYPE == 'LIVE':
    DEBUG = False
    DEFAULT_HOST = ''
else:
    DEBUG = True
    DEFAULT_HOST = '50.56.82.194'
    EMAIL_HOST = DEFAULT_HOST

1👍

I can recommend this repository.

You can modify it to support Django projects.

Vagrantfile updates:

  config.vm.define "web1", primary: true do |web1_config|
    web1_config.ssh.forward_agent = true

    # Create a private network, which allows host-only access to the machine
    web1_config.vm.network "private_network", ip: "192.168.11.10"
    web1_config.vm.hostname = "web1.#{domain}"

    web1_config.vm.provision "shell", path: "provisioners/shell/python.setup.sh"
    web1_config.vm.provision "shell", path: "provisioners/shell/application.setup.sh"
  end

Then add a provisioners/shell/application.setup.sh file with the following content:

#!/bin/bash

local_user=vagrant

if [ ! -n "$(grep "^bitbucket.org " /home/$local_user/.ssh/known_hosts)" ]; then 
    ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts 2>/dev/null;
fi

if [[ ! -d "/home/$local_user/app" ]]; then
    git clone git@bitbucket.org:czerasz/sample-django-app.git /home/$local_user/app

    chown -R $local_user:$local_user /home/$local_user/app

    su - $local_user -c "source /usr/local/bin/virtualenvwrapper.sh && mkvirtualenv sample-django-app-env && workon sample-django-app-env && pip install -r /home/$local_user/app/requirements.txt"
fi

Change the repository address (git@bitbucket.org:czerasz/sample-django-app.git) and make also sure that you have a requirements.txt in the root of your git repository. Run vagrant up.

Vagrant will start two machines:

  • web1 with your django project
  • db1 with a PoestgreSQL database

If you still have issues add the following to your Vagrantfile:

web1_config.ssh.private_key_path = [ '~/.vagrant.d/insecure_private_key', '~/.ssh/bitbucket' ]

And execute this command on your host (the machine where you run vagrant):

ssh-add ~/.ssh/bitbucket

The ~/.ssh/bitbucket is the ssh private key which you use for bitbucket. It can be ~/.ssh/id_rsa or something different depending how you configured it.

Leave a comment