TemplateDoesNotExist位于/​​ at templates / index.html

时间:2016-05-02 11:13:04

标签: python django

非已发布的解决方案似乎对我有用。

这是我的项目目录。

.
|-- 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'),
)

这是我的views.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/我在/"&#34获得" TemplateDoesNotExist

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时。页面呈现。

2 个答案:

答案 0 :(得分:0)

不要包含“模板”前缀。

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

另外,如评论中所述,TEMPLATE_DIRS已被删除:使用TEMPLATES字典。

答案 1 :(得分:0)

你的模板路径中不需要templates/,因为你告诉Django已经通过你的设置查看了dir。另外,context_instance=RequestContext(request)已被弃用,你只需要返回一个包含你的上下文的dict(在你的情况下是一个空的dict?):

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