Django:TemplateDoesNotExist 在/.../

新手上路,请多包涵

我正在尝试呈现一个简单的页面,但我遇到了一个问题。

 TemplateDoesNotExist at /pages/
{}

找不到模板文件夹。这是我的 settings.py 配置

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'pages/template')],
        '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',
            ],
        },
    },
]

我做错了什么吗?

-------------- 命令指令 ————–

 Internal Server Error: /pages/
Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python34\Scripts\src\pages\views.py", line 6, in index
    return render('index.html', context)
  File "C:\Python34\lib\site-packages\django\shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "C:\Python34\lib\site-packages\django\template\loader.py", line 96, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Python34\lib\site-packages\django\template\loader.py", line 43, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: {}
[12/Jan/2016 20:51:57] "GET /pages/ HTTP/1.1" 500 74646

- - - - - - 更新 - - - - - - -

文件树

在此处输入图像描述

视图.py

 from django.shortcuts import render

# Create your views here.
def index(request):
    context = {}
    return render(request, 'index.html', context)

原文由 Hiroyuki Nuri 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 426
2 个回答

当您调用 --- 时,您忘记了第一个参数 request render

 return render(request, 'index.html', context)

您可以有多个模板目录,例如 src/templatepages/template 。如果你想要一个 src/template 目录,那么你需要将它包含在你的 DIRS 选项中。

     'DIRS': [os.path.join(BASE_DIR, 'templates')],

您不需要在 pages/templates DIRS 目录中的 --- 应用程序加载器将找到该目录中的模板,因为您有 APP_DIRS Truepages 在你的 INSTALLED_APPS 设置中。

原文由 Alasdair 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可能忘记在 Installed_Apps 设置中添加您的应用程序。

INSTALLED_APPS = [

 'your_app'

]

原文由 Nitish Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏