Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Django restart runserver on template change?

When I make a modification in a python source file in my project, Django detects that and restart runserver itself. But when I modify a django template, I have to kill runserver and restart it again : how can I do to have runserver restart automatically on template change ?

like image 781
Eric Avatar asked Dec 01 '10 08:12

Eric


3 Answers

The file will by default be read from disk on every request, so there is no need to restart anything.

There is a caching template loader, but it is disabled by default. See the documentation for more info.

like image 169
knutin Avatar answered Nov 06 '22 11:11

knutin


Run touch against one of the Python source files.

Because runserver monitors .py files for changes, it does not restart for a change in the templates (.html). You can trigger this restart by virtually editing any of the .py files using the touch command, which refreshes its date modified and leaves all the other contents the same.

like image 26
Ignacio Vazquez-Abrams Avatar answered Nov 06 '22 11:11

Ignacio Vazquez-Abrams


Another solution is to make sure you have debug set to true inside of you TEMPLATES config in settings.py

DEBUG = True

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['templates/'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG,
        },
    },
]

When debug is False you have to manually restart the server to see any changes made to templates (since they don't automatically trigger a restart)

like image 8
TheRightChoyce Avatar answered Nov 06 '22 11:11

TheRightChoyce