获取路径中的每个文件夹

布鲁诺·塞尔克

我只想告诉我的 Django 项目,它想要的 .html 文件可以在“/templates”内的任何文件夹中找到。我知道这可以用一个简单的模式来完成,比如

allfolders = "/templates/*"

或者

allfolders '/templates/$'

或者

allfolders = 'templates/^$'

我真的不记得这样做的正确方法,它只是一个简单的字符串操作,可以告诉“文件位于文件夹 '/templates' 内的任何文件夹内,不需要任何导入。那是因为我想组织我的模板文件夹,因此我可以在其中包含不同的文件夹,以指出这是什么类型的 html 或来自哪个项目,诸如此类,组织建议。

感谢您的关注,对于缺少代码深表歉意。

沙米尔·卡西夫

您不需要为模板文件夹制作任何正则表达式或可能的匹配字符串。Django,默认在模板文件夹中搜索可能的子目录和 html 文档只需将您的 html 文档放在那里,如果需要,请创建子目录。您需要做的就是将从urls.pyin收到的请求传递views.pytemplates/subdirectory/document.html像这样的东西:

def home(request):
    if request.method == "GET":
        return render(request, 'subdirectory/document.html', {})

字符串subdirectory/document.hml表示templates/subdirectory/document.html

更新:如果您尝试将请求从urls.pyusing直接传递给可能的 html 文档direct_to_templates,您可以通过添加如下斜杠来实现:

网址.py

from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
(r"^$", direct_to_template, {"template": "subdirectory/document.html"}),
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章