获取路径中的每个文件夹

2024-04-18 19:29:25 发布

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

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

allfolders = "/templates/*"

或者

allfolders '/templates/$'

或者

allfolders = 'templates/^$'

我真的不记得正确的方法了,它只是一个简单的字符串操作,可以告诉“文件是在任何文件夹内的文件夹'/模板'没有任何导入被需要。这是因为我想组织我的模板文件夹,这样我就可以在里面有不同的文件夹,告诉我这是什么类型的html,或者来自什么项目,诸如此类的东西,组织建议

感谢您的关注,也很抱歉没有代码


Tags: 文件项目django方法字符串代码文件夹模板
1条回答
网友
1楼 · 发布于 2024-04-18 19:29:25

您不需要为templates文件夹创建任何正则表达式或可能的匹配字符串。Django,默认情况下,在templates文件夹中搜索可能的子目录和html文档。只要把你的html文档放在那里,如果需要的话,创建子目录。您只需将从views.py中的urls.py收到的请求传递到templates/subdirectory/document.html。像这样:

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

字符串subdirectory/document.hml表示模板/subdirectory/document.html

更新:如果您试图使用direct_to_templates直接将请求从urls.py传递到可能的html文档,可以通过添加如下斜杠来完成:

url.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"}),
)

相关问题 更多 >