【问题标题】:TemplateDoesNotExist at / at templates/index.htmlTemplateDoesNotExist at / at templates/index.html
【发布时间】:2016-08-27 03:18:05
【问题描述】:

不是已经发布的解决方案似乎对我有用。

这是我的项目目录。

.
|-- auto
|   |-- admin.py
|   |-- apps.py
|   |-- __init__.py
|   |-- migrations
|   |-- models.py
|   |-- views.py
|-- db.sqlite3
|-- manage.py
|-- mysite
|   |-- __init__.py
|   |-- settings.py
|   |-- test.py
|   |-- urls.py
|   |-- wsgi.py
|-- static
|   |-- index.html
`-- templates
    `-- index.html

这是我的 settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
   os.path.join(os.path.dirname(__file__), '../static/'),
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, '../templates'),
)

我也试过了,

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

这是我的观点.py

def render_landing_page(request, template="templates/index.html"):
    return render(request, template, context_instance=RequestContext(request))

这是我的 urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', views.render_landing_page),

]

在 127.0.0.1:8000/ 我得到一个“TemplateDoesNotExist at /”

Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.9.4
Exception Type: TemplateDoesNotExist
Exception Value:    
templates/index.html

但是,当我转到 127.0.0.1:8000/static/index.html 时。页面被渲染。

【问题讨论】:

标签: python django


【解决方案1】:

不要包含“模板”前缀。

 def render_landing_page(request, template="index.html"):

此外,如 cmets 中所述,已删除 TEMPLATE_DIRS:请使用 TEMPLATES 字典。

【讨论】:

    【解决方案2】:

    您不需要模板路径中的templates/,因为您已经告诉 Django 通过您的设置查看目录内部。此外,context_instance=RequestContext(request) 已弃用,您只需要返回一个包含您的上下文的字典(在您的情况下是一个空字典?):

    def render_landing_page(request, template="index.html"):
        return render(request, template, {})
    

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 1970-01-01
      • 2016-12-07
      • 2020-06-20
      • 2018-09-29
      • 2019-06-01
      • 2022-01-02
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多