迁移 django 0.96 到 1.2 时,遇到 {% include %} 和 {% extends %} 无法找到模板文件的问题。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DAOCLOUD
推荐学习书目
Python Cookbook
Using Google App Engine
推荐下载
Latest Google App Engine SDK
其他兼容技术
AppScale
darasion
V2EX    Google App Engine

迁移 django 0.96 到 1.2 时,遇到 {% include %} 和 {% extends %} 无法找到模板文件的问题。

  •  
  •   darasion 2011-03-10 00:22:15 +08:00 5519 次点击
    这是一个创建于 5381 天前的主题,其中的信息可能已经有所发展或是发生改变。
    试着迁移 0.96 到 1.2 ,发现一个问题,Google搜索无果。

    假设有这样的目录结构:
    project
    |--templates
    | |--ooxx
    | | |--aaa.html
    | |--bbb.html

    若 abc.html:
    {% include "../bbb.html" %}

    {% extends "../bbb.html" %}

    无效。

    这里有个解决办法,但是试过似乎仍然无效:
    https://groups.google.com/forum/#!msg/google-appengine-python/YaqfeygoiaI/RtDh9pL6XJQJ


    这个该怎样解决呢?
    2 条回复    1970-01-01 08:00:00 +08:00
    darasion
        1
    darasion  
    OP
       2011-03-10 10:18:15 +08:00
    自问自答:

    读了下django 0.96 和 1.2 的源码,对比了一下 django.template.loaders.file_system 这个模块。
    发现 Loader 类的 get_template_sources 方法中,文件路径的结合方式不同。

    0.96 使用的是普通的 os.path.join 方法,1.2 使用的是 django.utils._os.safe_join 方法,后者限制不允许使用 base path 外的路径。而 include 和 extends 标签会先从base path开始寻找模板(这也许是bug?),如果路径里边有 “../” 可能会直接跳出这个 base path ,于是就会触发找不到模板的异常:

    引用(django.utils._os.safe_join):
    def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    #...

    举例:
    >>> from django.utils._os import safe_join
    >>> safe_join('templates','../bbb.html')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python26\lib\site-packages\django\utils\_os.py", line 44, in safe_joi
    n
    raise ValueError('the joined path is located outside of the base path'
    ValueError: the joined path is located outside of the base path component
    >>>


    看来,在 django 1.2 模板中,不能 include 或 extends 上层目录的模板。
    lepture
        2
    lepture  
       2011-03-10 11:18:03 +08:00
    不能。。

    以上层作为 template dir 。

    render 的时候到下一层咯。 render("xxx/xxx.html")
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5733 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 06:16 PVG 14:16 LAX 22:16 JFK 01:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86