Friday, August 12, 2016

Set up development environment for multiple Django apps in vagrant that allows instant update

Leave a Comment

I have set up a virtual environment using vagrant, and virtualbox.

The virtual environment uses ubuntu 14.04 the production OS.

I want to have multiple django apps using this same virtual machine setup.

What I expect

Whenever I edit any python file or html on my macbook using my favorite editor (such as Vim or Sublime), the change is immediate whenever I press refresh on my browser (also in my macbook OS)

The django app is reached via a url such as http://djangoapp1. Other apps could be http://djangoapp2, etc

All these urls are actually pointing to the same vagrant machine.

When I want to edit the database (postgres), I only need to run valentina studios in my macbook and it will connect to the postgres database running inside the virtual machine.

What actually happened

I spent a lot of time getting multiple django apps to work in the same machine using nginx and uwsgi.

Then I realise whenever i make changes, the change is not reflected instantaneously. I needed to add a --touch-reload to the uwsgi process which is now started using upstart.

Even when I did add --touch-reload, I still had to deliberately go and "touch" a file whenever I want to reflect my code changes.

Even when it is done, the change doesn't always show up for some reason. It only shows up when I did a proper vagrant halt and then vagrant up. Vagrant reload causes the entire app to crash.

The only way I can have the code change > refresh web page > see change cycle is when I use the native manage.py runserver

But that means I cannot easily switch between apps during development. I have to shut one down and then start another one.

If I did the same using CakePHP or any kind of PHP frameworks to build multiple apps in the same virtual machine, I can have the make code change > refresh page > see changes development cycle.

How do I achieve the same ease for multiple Django apps in the same virtual machine while using my home OS to write code?

2 Answers

Answers 1

--touch-reload

As I known this arg is used for a specific configuration .ini file. Please try with option --py-autoreload 1.

At development phase, I think you can use Nginx proxy directly to default runserver or runserver_plus (please check django-extensions)

Answers 2

The following snippet will use Django's autoreloader to detect code changes, and hook into uWSGI to reload the code whenever necessary:

import uwsgi from uwsgidecorators import timer from django.utils import autoreload  @timer(3) def change_code_gracefull_reload(sig):     if autoreload.code_changed():         uwsgi.reload() 

You can put this code in your wsgi.py file before get_wsgi_application(). The decorator will register the function to be run every 3 seconds by uWSGI.

(Original source)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment