当我在项目中的python源文件中进行修改时,Django检测到并重启runserver本身.但是当我修改django模板时,我必须杀死runserver并重新启动它:如何在模板更改时自动重启runserver?
knu*_*tin 18
默认情况下,每次请求都会从磁盘读取该文件,因此无需重新启动任何内容.
有一个缓存模板加载器,但默认情况下它被禁用.有关详细信息,请参阅文档.
运行touch
对Python源文件之一.
因为runserver
监视.py文件的更改,它不会重新启动以更改模板(.html).您可以通过使用touch
命令虚拟编辑任何.py文件来触发此重新启动,该命令刷新其修改日期并使所有其他内容保持不变.
为了增加knutin的回答,您所面临的问题正好由FetchFromCacheMiddleware引起的,因此所有你需要做的就是在settings.py文件禁用如下内容:
settings.py:
MIDDLEWARE_CLASSES = (
...
#'django.middleware.cache.FetchFromCacheMiddleware',
...
)
Run Code Online (Sandbox Code Playgroud)
另一个解决方案是确保您已debug
在TEMPLATES
配置中设置为 truesettings.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,
},
},
]
Run Code Online (Sandbox Code Playgroud)
当debug
为 False 时,您必须手动重新启动服务器以查看对模板所做的任何更改(因为它们不会自动触发重新启动)