V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
4thmagi
V2EX  ›  Django

django 网页模板继承问题

  •  
  •   4thmagi · Apr 17, 2018 · 3665 views
    This topic created in 2942 days ago, the information mentioned may be changed or developed.
    在使用 Django 进行 web 开发时,往往会构造一个基础框架模板即 base.html,而后在其子模板中对它所包含站点公用部分和定义块进行重载。
    我的 django 版本是 2.0.3。我在包含 manage.py 文件的根目录下建立了文件夹 templates,在该文件夹中新建了 base.html。在 app 的 templates 文件夹下的 html 文件中继承了 base.html。就写了这么一行代码 {% extends 'base.html' %}
    但是在打开网页的时候报错内容如下:
    TemplateDoesNotExist at /login/login/
    base.html
    我将 base.html 复制到 app 的 templates 文件夹下依然报错,请问我该怎么修改?
    4 replies    2018-04-17 22:04:22 +08:00
    kaikai5601
        1
    kaikai5601  
       Apr 17, 2018 via iPhone
    setting 里面设置没设置好吧,模板默认目录
    shuizhengqi
        2
    shuizhengqi  
       Apr 17, 2018
    报错都这么明显了,找不到模板。你自己 setting 里面路径没设对
    4thmagi
        3
    4thmagi  
    OP
       Apr 17, 2018
    @kaikai5601 非常感谢。我在 settings.py 的 Templates[]中加了如下代码:
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    结果还是会报错。
    我在百度上搜索了如何 django 设置模板默认目录,得到的方案是在末尾加一句 TEMPLATES_DIRS = [
    os.path.join(BASE_DIR, "mysite\\templates").replace('\\','/'),
    ]
    请问 django2.0 应该如何设置模板默认目录?
    chengxiao
        4
    chengxiao  
       Apr 17, 2018   ❤️ 1
    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',
    'django.template.context_processors.static',
    ],
    },
    },
    ]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4264 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 00:56 · PVG 08:56 · LAX 17:56 · JFK 20:56
    ♥ Do have faith in what you're doing.