【问题标题】:Get every folder inside path获取路径中的每个文件夹
【发布时间】:2018-03-16 03:08:32
【问题描述】:

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

allfolders = "/templates/*"

allfolders '/templates/$'

allfolders = 'templates/^$'

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

感谢您的关注,抱歉缺少代码。

【问题讨论】:

    标签: python django python-3.x path string-matching


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我不确定您要达到的目标。但请确保不要在templates 的开头包含斜线。从/templates/templates/....
    • 我会尝试一下,但这是我想要做的,非常感谢
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多