11

I'm trying to setup a local Django dev environment using VMs enabled with Vagrant but I'm not sure what's the best way to go about it.

I did a git clone for Django files from production server and installed all the modules that the production server has on my local VM. I wanted to avoid installing a database on my local VM but ran into some problems with the sessions. The local machine is using SESSION_COOKIE_DOMAIN='localhost' and the production is using SESSION_COOKIE_DOMAIN='.mydomain.com' so that creates some confusion.

Not to mention that on the setting.py on my dev environment I had to change IPs to point to the public IP address of the database (thus poking a hole on the security) while my production settings.py is using the local IPs so I ended up using different settings.py files.

I can continue experimenting with new methods but I really have to get going with the project and I'm pretty sure some people had this figured out already.

So how did you setup your Django dev environment?

Cœur
  • 37,241
  • 25
  • 195
  • 267
avatar
  • 12,087
  • 17
  • 66
  • 82
  • Questions on StackOverflow should be definitively answerable. There's dozens of ways to setup a Django environment and most of it boils down to personal preference of individualized needs of the given application. If you want to get some ideas, Google is the place for that. – Chris Pratt Nov 16 '11 at 15:08
  • @Chris Pratt I appreciate your input. – avatar Nov 16 '11 at 15:22
  • This question has been asked several times already. See http://stackoverflow.com/questions/1626326/how-to-manage-local-vs-production-settings-in-django and http://stackoverflow.com/questions/88259/how-do-you-configure-django-for-simple-development-and-deployment – Mark Lavin Nov 16 '11 at 16:47

3 Answers3

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/

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • Considering the warning on the front page of djangobook, why are you recommending it? – pydanny Dec 20 '13 at 17:49
  • Hi pydanny, I have updated the repo in my GitHub to point to Tango With Django, and to your excellent Two Scoops of Django book for more advanced topics. Thanks for the heads up. – FlipperPA Jan 23 '14 at 15:57
  • @FlipperPA : Will it detect changes and reload `django server` if i make change in any `.py` files on my guest machine? I am trying this and it doesn't reload django server. – Lal Feb 10 '17 at 13:23
  • It should if you have your project in the shared folder. There's a much newer version available here: https://github.com/FlipperPA/django-python3-vagrant – FlipperPA Feb 10 '17 at 13:39
  • Just confirmed that file status `last modified` get updated on host machine after i make a change to file on guest machine. Looks like some issue with `Django 1.5` autoreload – Lal Feb 10 '17 at 14:11
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.

czerasz
  • 13,682
  • 9
  • 53
  • 63
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
Dave LeBlanc
  • 251
  • 2
  • 7
  • Do you have a separate dev database that's similar to the production DB or you just point your Django dev code to the production DB? Thank you for putting some light on the subject. – avatar Nov 16 '11 at 16:50