How to automatically reload Django when files change?

59,327

Solution 1

You don't need a browser extension to accomplish auto refreshes. Take a look at https://github.com/tjwalch/django-livereload-server.

I posted a more extensive answer about this at https://stackoverflow.com/a/36961244/2950621

It works by using a manage.py command (server) to monitor your .js and other static files. The server sends a signal to the browser via websockets. There is some client-side code injected on every page. The injected code responds to the signal and refresh the browser.

Solution 2

Install this django app:

pip install django-livesync

On your django settings file add something like:

INSTALLED_APPS = (
    '...',
    'livesync',
    'django.contrib.staticfiles',
    '...',
)

MIDDLEWARE_CLASSES = (
    'livesync.core.middleware.DjangoLiveSyncMiddleware',
)

Beware to register 'livesync' before 'django.contrib.staticfiles' if you are using it.

Now, just start your development server:

python manage.py runserver

Check this out for more details: https://github.com/fabiogibson/django-livesync

Solution 3

Using python manage.py runserver is what most use. You'll have to use another tool like: http://livejs.com/ to refresh the browser itself since Django really isn't aware of it.

Solution 4

Frustrated with all the explicit refreshes, I created a browser extension, for both Firefox and Chrome, to automate this. The extension works with a Django app that you add to your app list in INSTALLED_APPS. You can find out more at the github repo.

Though the repo has entire source code, the extensions are also available in the respective web store. Just search for 'Django Auto Refresh'. With these, you just need to copy the app into our project's folder and include it via INSTALLED_APPS. I wanted to add a pip setup script, but haven't found the time to do it.

HTH. Apologies if this sounds like self promotion.

Solution 5

I tried several answers here. But the browser did not seem to show the recent changes of the code. It worked for me when I opened Chrome in Incognito Mode.

Share:
59,327
metakermit
Author by

metakermit

Hey! I’m Dražen. I am a developer and founder of the indie web development studio Punk Rock Dev. I’m building web applications and doing data analysis in Python, JavaScript and other technologies professionally since 2009. I mostly work with technologies like JavaScript, Python, Linux, PostgreSQL… If you are interested in working with me, feel free to reach out through my company or contact me directly.

Updated on October 30, 2021

Comments

  • metakermit
    metakermit over 2 years

    How to automatically monitor .py, .js and other source code files to restart a Django (or any other for that matter) application and refresh the browser when the source changes? This is possible in Rails using guard, in JS apps using grunt-contrib-watch and the accompanying livereload browser plugin. How can I do it for Python web apps such as Django?

    I start my Django server with

    foreman start
    

    this is my Procfile:

    web: newrelic-admin run-program gunicorn app.wsgi
    

    as suggested by the Heroku/Newrelic docs or the usual

    python manage.py runserver
    

    The runserver method does restart the server on .py source changes, but not the browser and doesn't watch other files - I could run guard alongside it, but then I have two processes I have to take care of, whereas grunt or rake offer unified interfaces. I'm wondering what is the recommended way of doing this among Python developers?

    I could not find any detailed, comprehensive documentation on this - only incomplete discussions here and there.

  • kiloton
    kiloton over 2 years
    I realize your repo and this answer are four years old, but just using your instructions my Firefox (ver. 94) won't reload the DJango templates. Is more configuration needed in 2021?
  • RVFET
    RVFET about 2 years
    Not working, probably outdated. Not sure tho, I just tried and it didn't worked for me