2016-05-02 133 views
-1

非已發佈的解決方案似乎適用於我。TemplateDoesNotExist at/at templates/index.html

這是我的項目目錄。

. 
|-- 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/我得到

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

一個 「在/ TemplateDoesNotExist」 但是,當我去127.0.0.1:8000/static/index.html。該頁面已呈現。

+0

'TEMPLATE_DIRS'已被棄用。改用https://docs.djangoproject.com/en/1.9/ref/settings/#templates。 – Selcuk

回答

0

請勿包含「模板」前綴。

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

此外,正如註釋中所述,TEMPLATE_DIRS已被刪除:使用TEMPLATES字典。

0

您的模板路徑中不需要templates/,因爲您告訴Django已經通過您的設置查看了目錄。此外,context_instance=RequestContext(request)已被棄用,你只需要返回包含您的上下文的字典(在你的情況下,一個空的字典?):因爲Django的1.8

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