How to livereload Django templates?
Asked Answered
A

4

22

How can I livereload Django templates?

Annatto answered 29/9, 2013 at 2:45 Comment(3)
@jthill, I am looking for constructive and practical answers and solutions. Google search results didn't provide them.Annatto
Alright, let me rephrase that: what's the best thing you've found so far? Show that, and say where you're stuck. As it is now, "constructive and practical answers" is better spelled "someone to research and write an article tailored to my specific needs, which I decline to state".Cupel
Hi @jthill, to "livereload Django templates" means that when I change my Django templates the change should be automatically propagated to the browser so that I don't have to refresh it myself. I am asking this question because I haven't seen any such solutions from the Google/DuckDuckGo search results. I personally haven't tried to get it working. How can you help?Annatto
A
3

I found a very simple solution by using Grunt and a livereload browser extension.

Annatto answered 22/11, 2013 at 2:24 Comment(0)
B
18

I maintain the package django-livereload-server that adds both the livereload javascript and provides a livereload server in an easy django way. All you need to do is:

  • pip install django-livereload-server
  • add 'livereload' to INSTALLED_APPS
  • add 'livereload.middleware.LiveReloadScript' to MIDDLEWARE_CLASSES
  • start the livereload server/file watcher with ./manage.py livereload.
Brothers answered 9/9, 2016 at 12:2 Comment(3)
i'm getting an issue where it's indicating 0 watchers when it updates. To get this straight, I run ./manage.py livereload on a separate port and i should be able to point the browser to the runserver path (default 127.0.0.1:8000) and it should automatically update right? I tried to go to the livereload port just to make sure but was getting an error 404. How do I add the browser as a watcher?Lurk
@DavidTorrey hey, i followed this too, and i'm getting a 404 error, how did you solve it ?Ferrel
@HammiCloud what I found is that it wasn't checking the directory correctly. I used the absolute path just to make sure, but it looks something like: "./manage.py livereload path/to/folder after that you go to the ip/port you set up in django (not livereload) and it should auto updateLurk
T
12

I found a Python native solution which is easy to setup and works well, Python LiveReload (doc).

pip install livereload
livereload /path/to/monitor

You still have to install the browser plugin. The plugin and LiveReload use port 35729 to talk (customizable), but you need to point the browser to the port specified via runserver. For example:

python manage.py runserver example.com:8000

In this case the live reloaded url is http://example.com:8000, and when you change your templates the page is refreshed automatically.

As a final note, Python live reload can also be used programmatically and supports wsgi applications.

Thalassa answered 9/6, 2014 at 22:28 Comment(1)
I set up django-live-reload in my dev environment and no browser plugin is required. django-live-reload is supposed to be based upon the python livereload package so maybe the browser plugin is no longer required. They use websockets which must use technology built into browsers these days?Sliver
H
4

You can use python-livereload like this:

pip install livereload
livereload project/static

And in order to make this work add this snippet into your base.html:

<script type="text/javascript" src="http://127.0.0.1:35729/livereload.js"></script>

Then run ./manage.py runserver and it should work.

Hydrastine answered 22/7, 2016 at 8:57 Comment(0)
A
3

I found a very simple solution by using Grunt and a livereload browser extension.

Annatto answered 22/11, 2013 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.