python Django:按目录指定基本模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/671369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: specifying a base template by directory
提问by RexE
I'm working on a Django site that has multiple sections and subsections. I'd like to have several depths of template inheritance: a base template for the whole site, one base template for each section that inherits from the root base template, and so on. Here's a simplified version of my desired directory structure:
我正在一个有多个部分和子部分的 Django 站点上工作。我想有几个深度的模板继承:整个站点的基本模板,从根基本模板继承的每个部分的一个基本模板,等等。这是我想要的目录结构的简化版本:
base.html
section1/
base.html
section2/
base.html
section3/
base.html
What I would desire is for all the files under section1/
to contain something like {% extends "base.html" %}
, meaning they would extend section1/base.html
. section1/base.html
would contain something like {% extends "../base.html" %}
, meaning that it would extend the root-level base file. However, I couldn't find anything in the documentation suggesting this was possible, and I couldn't get Django to distinguish between "../base.html"
and "base.html"
. ({% extends "../base.html" %}
throws an error.) I suppose one workaround would be to rename all base files base_SECTIONNAME.html
, and update all the files that inherit from them, but I am concerned that this might become difficult to maintain as my site becomes bigger and sections change names, etc. I would prefer a solution that takes advantage of the natural hierarchy specified by directories and subdirectories.
我希望下面的所有文件section1/
都包含类似的内容{% extends "base.html" %}
,这意味着它们会扩展section1/base.html
. section1/base.html
将包含类似的内容{% extends "../base.html" %}
,这意味着它将扩展根级基本文件。但是,我在文档中找不到任何表明这是可能的内容,并且我无法让 Django 区分"../base.html"
和"base.html"
。({% extends "../base.html" %}
引发错误。)我想一种解决方法是重命名所有基本文件base_SECTIONNAME.html
,并更新从它们继承的所有文件,但我担心随着我的网站变大和部分更改名称等,这可能变得难以维护. 我更喜欢利用目录和子目录指定的自然层次结构的解决方案。
Any ideas?
有任何想法吗?
回答by Oliver Andrich
May be I oversee something, but all you want can be accomplished with the django template system. All extends calls are relative to template directories.
可能是我监督了一些事情,但是你想要的一切都可以用 django 模板系统来完成。所有扩展调用都与模板目录相关。
In order for all base.html files in subdirectories to extend base.html, you just have to put a
{% extends "base.html" %}
into the files. section1/base.html would would look like that.{% extends "base.html" %}
{# ... rest of your code ...#}
Now, to get the files from section1 to extend section1/base.htmlyou just have to put
{% extends "section1/base.html" %}
at the top of them. Same for section2, section3 and so on.
为了让子目录中的所有 base.html 文件扩展 base.html,您只需将 a
{% extends "base.html" %}
放入文件中。section1/base.html 看起来像那样。{% extends "base.html" %}
{# ... rest of your code ...#}
现在,要从 section1 获取文件以扩展section1/base.html,您只需将其放在
{% extends "section1/base.html" %}
它们的顶部。第 2 节、第 3 节等也是如此。
It is just that simple, but might not totally obvious in the documentation.
就是这么简单,但在文档中可能并不完全明显。
I hope, I understood your question.
我希望,我理解你的问题。
回答by David Berger
The accepted answer will work, but I do recommend using variable names to keep track of section structure. My personal preference would be a context processor. If, for example, your site's section organization is transparently reflected in the url, try something like:
接受的答案会起作用,但我建议使用变量名称来跟踪部分结构。我个人的偏好是上下文处理器。例如,如果您网站的部分组织透明地反映在 url 中,请尝试以下操作:
# It may be convenient to make this function live in or near your url conf.
def convert_url_path_to_folder_path(path):
# fill in the magic here
def sub_folder_available(request):
folder = convert_url_path_to_folder_path(request.path)
return {'subsection': folder, 'local_base':folder+'/base.html'}
Then in your template, just call
然后在您的模板中,只需调用
{% extends local_base %}
There are probably a dozen other ways to do this, but the main thing is to think about avoiding hard-coding the folder name into the template. This will get you a lot of mileage, especially since you can just drag and drop template between sections if they happen to be similar enough. Another thing you might add insert is:
可能有十几种其他方法可以做到这一点,但主要是考虑避免将文件夹名称硬编码到模板中。这将为您带来很多好处,特别是因为您可以在部分之间拖放模板,如果它们足够相似的话。您可能会添加插入的另一件事是:
def sub_folder_available(request):
folder = convert_url_path_to_folder_path(request.path)
# Check if local base exists:
if os.access(folder+'/base.html',os.F_OK):
base = folder+'/base.html'
else:
# revert to your global base
base = 'base.html'
return {'subsection': folder, 'base':base}
The nice advantage of this strategy is of course that you can get a fly-weight section up and running without any local base template at all.
这种策略的好处当然是你可以在没有任何本地基础模板的情况下启动并运行一个 Fly-weight 部分。
回答by vb64
You can use this library: https://github.com/vb64/django.templates.relative.path
你可以使用这个库:https: //github.com/vb64/django.templates.relative.path
Just write in your templates as follows:
只需在您的模板中写入如下:
{% load relative_path %} {% extends ".base.html" %}
{% load relative_path %} {% extends ".base.html" %}
this will extend template "base.html", located in the same folder, where your template placed
这将扩展模板“base.html”,位于您的模板所在的同一文件夹中
{% load relative_path %} {% extends "...base.html" %}
{% load relative_path %} {% extends "...base.html" %}
extend template "base.html", located at two levels higher
扩展模板“base.html”,位于更高的两个级别
same things works with 'include' tag.
同样的事情适用于“包含”标签。