Django 模板自动重新加载 [英] Django Template Autor-reload
问题描述
每当我在开发中编辑模板文件时,如何禁止 Django 3.2 重新加载服务器.这实际上降低了我的工作效率,因为重新加载需要 1-4 秒.
How can i disable Django 3.2 from reloading the server whenever i edit template files in development. This is actually making my productivity slow because it takes 1-4 secs to reload.
推荐答案
这是一个错误,目前在 Django 的问题跟踪器上有一张票,请参阅 票号 #32744.这将在以后的版本中得到解决,但在那之前,从票本身的讨论中我们可以看到问题源于较新的 Django 项目使用 pathlib.Path
而较旧的项目仅使用字符串的问题路径.您可以简单地更改您的项目设置以使用 pathlib.Path
来解决此问题(至少对您自己而言).在 settings.py
中进行以下更改(保持其他设置不变):
This is a bug that currently has a ticket on Django's issue tracker, see Ticket #32744. This will be resolved in further versions, but until then from the discussion on the ticket itself we can see that the issue stems from the problem that newer Django projects use pathlib.Path
while older projects just used strings for the paths. You can simply change your project settings to use pathlib.Path
to solve this issue (at least for yourself). Make the following changes in settings.py
(keep other settings unchanged):
from pathlib import Path
# Change the setting `BASE_DIR`
BASE_DIR = Path(__file__).resolve().parent.parent
# In the `DIRS` list of the setting `TEMPLATES`, for keeping it relevant I would only show that key
TEMPLATES = [
{
'DIRS': [BASE_DIR / 'templates'] # I don't show the other keys, but don't remove them!
},
]
# the `DATABASES` setting if you use sqlite3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
这篇关于Django 模板自动重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!