Django Template does not exist error, although it shows 'file exists'

与世无争的帅哥 提交于 2019-12-17 19:24:33

问题


I am not able to render any html pages in Django 1.7. My 'index.html' is in 'project/seatalloc/templates/index.html' and my view.py in project/seatalloc/views.py looks like:

 def index(request):
       return render(request, 'index.html', dirs=('templates',)) 

project/project/settings.py has templates dirs set:

TEMPLATE_DIRS = (
    '/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates',

)

urls.py:

urlpatterns = patterns('',
    url(r'^seatalloc/', include('seatalloc.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I have tried to follow the documentation strictly, yet can't figure out if Django detects the file, why am I getting TemplateDoesNotExist at /seatalloc/ error. I am new to Django, could someone please help.


回答1:


If - as in your case - you get a TemplateDoesNotExist error and the debug page states "File exists" next to the template in question this usually (always?) means this template refers to another template that can't be found.

In your case, index.html contains a statement ({% extends %}, {% include %}, ... ) referring to another template Django cannot find. Unfortunately, as of Django 1.8.3, the debug page always names the base template, not the one Django can't find.




回答2:


Try like this,

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



回答3:


First of all don't use static path (fixed path) in template dirs in settings.py, use:

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

TEMPLATE_DIRS = (
      BASE_DIR +'/Templates',


  )

And template directory should be in project directory i.e in which manage.py file exists.

And in view.py use render_to_response instead of just render.

return  render_to_response("index.html") 


来源:https://stackoverflow.com/questions/26604294/django-template-does-not-exist-error-although-it-shows-file-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!