django5使用模版语法无法访问页面

今天使用模版语法无论如何都无法使用,一直提醒路径不对,找不到base.html,最后发现是犯了一个经常性错误,如下:

 解决办法:

其实是文件结构不对,我一直把base.html放在了我的应用nav的templates里面,实际应该是放在总项目里面才对,以下是正确结构:

your_project/
├── templates/
│   └── components/
│       └── base.html  # 父模板
├── nav/
│   └── templates/
│       └── nav/
│           └── index.html  # 子模板

 成功访问

TEMPLATES 文件如下:

 

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # 确保这一行存在
        ],
        "APP_DIRS": True,  # 允许从应用的 templates/ 加载模板
        "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",
            ],
        },
    },
]

 

posted @ 2025-04-06 02:00  liuyunhong  阅读(32)  评论(0)    收藏  举报