【问题标题】:Django Template Autor-reloadDjango 模板自动重载
【发布时间】:2026-01-31 11:50:01
【问题描述】:

当我在开发中编辑模板文件时,如何禁用 Django 3.2 重新加载服务器。这实际上降低了我的工作效率,因为重新加载需要 1-4 秒。

【问题讨论】:

    标签: django templates reload


    【解决方案1】:

    以下是工作方式:

    for x in TEMPLATES:
        if 'OPTIONS' not in x:
            x['OPTIONS'] = {}
    
        x['OPTIONS']['debug'] = True  # True: reload template on change
                                      # False: no reload on change
    

    【讨论】:

      【解决方案2】:

      这是一个错误,目前在 Django 的问题跟踪器上有一张票,请参阅Ticket #32744。这将在进一步的版本中得到解决,但在此之前,从票证本身的讨论中我们可以看到问题源于较新的 Django 项目使用 pathlib.Path 而较旧的项目只使用字符串作为路径的问题。您可以简单地更改您的项目设置以使用pathlib.Path 来解决此问题(至少对您自己而言)。在settings.py中进行如下修改(其他设置保持不变):

      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',
          }
      }
      

      【讨论】:

        【解决方案3】:

        尝试:

        python manage.py runserver --noreload
        

        文档是here

        【讨论】: