python瓶子web服务器缓存html文件

2024-04-24 22:50:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python中的瓶子web服务器。调试时,我希望对伪html模板文件进行更改,然后重新加载页面以在浏览器中看到它。我似乎无法从模板中加载新的缓存文件。在

我的情况是:

第一种方法

  1. 运行web服务器,启动主.tpl.htlml在
  2. 更改的文件名 二级.tpl.html第二次。第三方物流.html在
  3. 打开二级.tpl.html(已不存在)
  4. 得到404错误也就是说,一切都很好

第二种方法

  1. 运行web服务器,启动主.tpl.htlml在
  2. 打开二级.tpl.html,工作正常
  3. 更改的文件名二级.tpl.html第二次。第三方物流.html在
  4. 重新加载二级.tpl.html(已不存在)
  5. 拿到原件二级.tpl.html不再存在的网页

我已经强迫浏览器第二次请求该文件(不使用它自己的缓存),并且,通过python调试器(PyCharm),我可以看到瓶子web服务器至少假装在做它应该对模板文件执行的所有操作。在


Tags: 文件方法服务器模板web瓶子文件名html
1条回答
网友
1楼 · 发布于 2024-04-24 22:50:29

来自http://bottlepy.org/docs/dev/stpl.html#bottle.SimpleTemplate

Just keep in mind that compiling and rendering templates are two different actions, even if the template() helper hides this fact. Templates are usually compiled only once and cached internally, but rendered many times with different keyword arguments.

但是http://bottlepy.org/docs/dev/tutorial.html#templates声明:

Caching

Templates are cached in memory after compilation. Modifications made to the template files will have no affect until you clear the template cache. Call bottle.TEMPLATES.clear() to do so. Caching is disabled in debug mode.

你可以在http://bottlepy.org/docs/dev/tutorial.html#debug-mode找到

bottle.debug(True)

In this mode, Bottle is much more verbose and provides helpful debugging information whenever an error occurs. It also disables some optimisations that might get in your way and adds some checks that warn you about possible misconfiguration.

Here is an incomplete list of things that change in debug mode:

The default error page shows a traceback.
Templates are not cached.
Plugins are applied immediately.

如果这仍然没有帮助,可以浏览一下https://github.com/bottlepy/bottle/blob/master/bottle.py#L3937

class SimpleTemplate(BaseTemplate):
    def prepare(self,
            escape_func=html_escape,
            noescape=False,
            syntax=None, **ka):
        self.cache = {}
        ...

似乎可以清除缓存。在

相关问题 更多 >