2016-09-23 132 views
1

Python 2.7 & Django 1.10 我的模板存在,但我做錯了!TemplateDoesNotExist但它存在

TemplateDoesNotExist at /basicview/2/ 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>TEST</title> 
</head> 
<body> 
This is template_two view! 
</body> 
</html> 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/basicview/2/ 
Django Version:  1.10.1 
Exception Type:  TemplateDoesNotExist 
Exception Value:  

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>TEST</title> 
</head> 
<body> 
This is template_two view! 
</body> 
</html> 

Exception Location:  /home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg/django/template/loader.py in get_template, line 25 
Python Executable: /home/i/djangoenv/bin/python 
Python Version:  2.7.11 
Python Path:  

['/home/i/djangoenv/bin/firstapp', 
'/home/i/djangoenv/lib/python2.7', 
'/home/i/djangoenv/lib/python2.7/plat-i386-linux-gnu', 
'/home/i/djangoenv/lib/python2.7/lib-tk', 
'/home/i/djangoenv/lib/python2.7/lib-old', 
'/home/i/djangoenv/lib/python2.7/lib-dynload', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-i386-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/home/i/djangoenv/local/lib/python2.7/site-packages', 
'/home/i/djangoenv/local/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg', 
'/home/i/djangoenv/lib/python2.7/site-packages', 
'/home/i/djangoenv/lib/python2.7/site-packages/Django-1.10.1-py2.7.egg'] 

Server time: Пт, 23 Сен 2016 15:43:30 +0000 

settings.py(os.path.join(BASE_DIR), '模板',或/家庭/ mainapp /模板)不工作..

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': ['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', 
      ], 
     }, 
    }, 

文章/人次。 PY我DEF的樣子:

def template_two(request): 
    view = "template_two" 
    t = get_template('myview.html') 
    html = t.render(Context({'name': view})) 
    return render(request, html, {}) 

我的文件:

mainapp/mainapp/settings.py 
mainapp/mainapp/article/views.py 
mainapp/templates/myview.html 

回答

1

的問題是,您要手動執行你的模板,並使用render快捷方式。您的get_template正在工作,但是當您撥打render(request, html, {})時,Django將html作爲文件名,並尋找名爲<!DOCTYPE html>\n<html>...的模板文件。

您應該手動呈現模板:

def template_two(request): 
    view = "template_two" 
    t = get_template('myview.html') 
    html = t.render({'name': view}) # Note you should use a plain dictionary, not `Context` on Django 1.8+ 
    return HttpResponse(html) 

或者,它更簡單使用render快捷。

def template_two(request): 
    view = "template_two" 
    return render(request, "myview.html", {'name': view}) 

你也應該改變你的DIRS設置回用os.path.join(BASE_DIR, 'templates')。使用字符串'templates'是行不通的。

+0

謝謝!它幫助我! – KingOfPing

1

在你settings.py你有'DIRS': ['templates'],

和路徑到您的模板是mainapp/templetes/myview.html

你有錯字templetes != templates。用模板重命名文件夾到templates

+0

同樣的錯誤:在TemplateDoesNotExist/basicview/2/ myview.html – KingOfPing

2

我建議你把你的temlates放在你的app裏。

你的文件將在這裏:

mainapp/mainapp/templates/myview.html 

請確保您添加mainappINSTALLED_APPS這樣的:

INSTALLED_APPS = [ 
    ... 
    'mainapp', 
] 
+0

沒有我的模板是mainapp不mainapp/mainapp – KingOfPing