【问题标题】:Is it possible to only specify the name of the "base.html" file in the {%extends%}, instead of "app/base.html"?是否可以只在 {%extends%} 中指定“base.html”文件的名称,而不是“app/base.html”?
【发布时间】:2021-07-30 16:50:09
【问题描述】:

我正在学习 Django,我偶然发现了一个问题: 是否可以从 helloworld.html 的 {% extends "app/base.html" %} 链接中创建 base.html 文件的路径 文件尽可能短? 所以我只需要写 {% extends "base.html" %} 来让它工作。

现在,如果我尝试这样做,python 将在 def helloworld(request): views.py 中的函数处抛出异常。

所以我的应用文件结构是:

├── app
│   ├── migrations
│   ├── static
│   ├── templates 
│      ├── app
│         ├── base.html
│         ├── helloworld.html
│   ├── views.py 
│   └── models.py 
├── DjangoProject
│   ├── __init__.py 
│   ├── urls.py 
│   ├── settings.py 
│   ├── templates 
├── manage.py

helloworld.html:

{% extends "app/base.html" %}

{% block head_title %} hello {% endblock %}

{% block content %} 
<p> hello world </p> 
{% endblock %}

views.py:

from django.shortcuts import render
from django.http import HttpRequest

def helloworld(request):
    hello_title = 'Test'
    assert isinstance(request, HttpRequest)
    return render(
        request,
        'app/helloworld.html',
        {
            'title_test':hello_title    
        }
    )

settings.py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
    'app',]
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

我尝试在 settings.py 文件中的 TEMPLATES 部分的 'DIRS:' 部分中包含另一个路径,但它没有似乎工作。 我发现的 django 文档和另一个 stackoverflow 帖子只讨论了添加“app/base.html”路径。

【问题讨论】:

    标签: python-3.x django path django-templates


    【解决方案1】:

    当然,您可以通过简单地使用{% extends template_name %}从您的视图发送它

    在你看来:

    return render(
        request,
        'app/helloworld.html',
        {
            'title_test': hello_title,
            'template_name': 'app/base.html'
        }
    )
    

    请记住,这不会使用您在渲染中提供的标题。 为了使用标题,它需要在模板中有一个具有该名称的变量。
    {% block head_title %} {{ title_test }} {% endblock %}

    就我个人而言,我的做法是将应用名称放在不同的变量中,例如:

    class MyView(View):
        template_name = f'{APP_NAME}/{TEMPLATE_NAME}'
        ...
        def get(self, request, *args, **kwargs)
            return render(
                request,
                'app/helloworld.html',
                {
                    'title_test': hello_title,
                    'template_name': self.template_name
                }
            )
    

    这两个变量都是我从不同文件导入的常量。

    【讨论】:

    • 谢谢你的回答,很清楚。您能否告诉我,在 settings.py 文件的“模板”部分中将另一个直接指向最后一个“app/”文件的路径添加到“DIR”中是否足以使 {% extends base.html %} 工作?我不明白为什么我的直接指向“app/”文件的路径在它指向其父文件“templates/”的情况下无法正常工作?
    • @LeBruceWayne 不太明白你的意思。除非您有一些独特的设置,否则您不应该真正触摸设置中的TEMPLATES 变量。 'DIRS' 应该保留为 [],除非您在每个应用程序目录中都有除 templates 之外的其他位置的模板目录。 APP_DIRS 也应保留为 True
    • 模板路径的默认构建方式为project/app/templates/templates 中的任何文件夹都需要在扩展或包含时写入。
    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多