TemplateDoesNotExist - file exists, no permissions issue

﹥>﹥吖頭↗ 提交于 2019-12-12 10:06:12

问题


I'm receiving this error when trying to render a template in django:

TemplateDoesNotExist

...
Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:

Here's my settings.py entry:

SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(SETTINGS_PATH, 'template'),
)

Here's my view that's being called:

def handler(request):
    if request.method == 'GET': 
        NewAppFormSet = modelformset_factory(Application)

    return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})

I've tried every possible combination of paths, adjusting permissions, etc. The real issue seems to be with the template loaders, as they are not recognizing any paths set forth in TEMPLATE_DIRS. I've hardcoded this string, to no avail. I've also ran python manage.py runserver with sudo / as root. I'm at a loss...


回答1:


I had a very similar issue which was driving me crazy. It turns out that the book I was using to learn Python/Django did not say that I had to set the templates directory in settings.py expressly. Very confusing. I am using Python 2.6 & Django 1.3.1 - maybe a previous version automatically found the templates dir. Anyway, after pulling my hair out for a day and reading a lot on this site I figured out that I had to replace the default TEMPLATE_DIRS assignment with the following in settings.py:

# Find templates in the same folder as settings.py.
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(SETTINGS_PATH, 'templates'),
)

Thanks stackoverflow!!




回答2:


I'm not sure what example I was following that specified the render_to_response shortcut method requires the views' request object to be passed, but it was as easy as changing it to:

return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet()})



回答3:


Try assigning SETTINGS_PATH with os.path.realpath:

SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))



回答4:


By any chance, you might have copy-pasted the TEMPLATE_DIRS you have now, did you leave the default one lower down the SETTINGS file?




回答5:


From my settings.py:

BASE_DIR = os.path.dirname(__file__)

def RELATIVE_PATH(*args):
    return os.path.join(BASE_DIR, *args)

TEMPLATE_DIRS = (
    RELATIVE_PATH('template'),
)



回答6:


what if you try

return render_to_response(request, "apps.html",{"new_app_form":NewAppFormSet})

instead of

return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})


来源:https://stackoverflow.com/questions/4975842/templatedoesnotexist-file-exists-no-permissions-issue

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