6

Why does django-admin.py runserver restart if certain files (urls.py)have changed, but not others (template files)?

Is there a way to specify which files runserver should monitor for changes, and restart if modifications are detected?

Mike
  • 58,961
  • 76
  • 175
  • 221
  • Related: [How to make Django restart runserver on template change? - Stack Overflow](https://stackoverflow.com/questions/4322624/how-to-make-django-restart-runserver-on-template-change) – user202729 Feb 04 '23 at 03:01
  • Potentially related question: [python 3.x - Gunicorn caching Flask Jinja2 templates when using render_template() - Stack Overflow](https://stackoverflow.com/questions/60681629/gunicorn-caching-flask-jinja2-templates-when-using-render-template) (if gunicorn is used then it doesn't reread every time, but this is on Flask) // other potentially related questions: https://stackoverflow.com/q/70515771/5267751 https://stackoverflow.com/q/48123986/5267751 – user202729 Feb 04 '23 at 03:12

2 Answers2

6

Because the template files are parsed on each request. They are not loaded in memory. But with .py files it's different as they are loaded in memory when the server starts so a restart is needed to reload them.

LE: runserver checks for changes only in the files that it loads/needs for the app to run. i.e. the settings.py file, ROOT_URLCONF specified in the setting files, the INSTALLED_APPS, etc.

I don't think there is a way to tell it to monitor certain file that is not loaded at runserver. And you wouldn't need that anyway. Why would you want to restart the app for a file that does not affect the execution of your app.

daniels
  • 18,416
  • 31
  • 103
  • 173
  • 5
    Thanks for the answer. The second part of my question asks how to tell Django to restart based on changes outside the core files. It's not limited to template files. For instance, I have a Python script `views.py` and runserver does not restart when it is modified. – Mike Jun 19 '11 at 01:09
  • Hi, I have a file name settings.py but is not the setting of django and not related to django. I dont know why if i update that file the django will restart. – ji-ruh Sep 29 '17 at 08:10
1

The OP expressed interest in restarting the runserver when template files are changed. It might be helpful to know that you can also force the browser to refresh a view created by templates if you use livereload protocol.

See my answer to this question about reflecting updates to javascript files used by templates to see how to set this up. It involves installing an add-on to the runserver feature in Django.

Community
  • 1
  • 1
nmgeek
  • 2,127
  • 1
  • 23
  • 31