1

所以我一直在尝试设置一个 django 设置模块来检查环境变量和加载设置。

这是我的设置模块的样子

/templates
    home.html

/settings
    base.py
    prod.py
    dev.py
    test.py

基础.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, "templates"),
]

urls.py

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
url(r"^$", direct_to_template, {'template' : 'home.html' }, name="home"),
)

当我将所有设置都放在一个文件中时,这工作得很好,但是由于我将文件拆分,我得到了错误:

TemplateDoesNotExist at /

home.html

Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/settings/templates/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/auth/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admindocs/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/grappelli/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/django/contrib/admin/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/pagination/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/djangosaml2/templates/home.html (File does not exist)
/Users/Tulsa/Apps/tulsa-applications-co/tulsa/tulsa/apps/profiles/templates/home.html (File does not exist)
/Users/Tulsa/.Apps/tulsa_io/lib/python2.7/site-packages/debug_toolbar/templates/home.html (File does not exist)

Using loader django.template.loaders.eggs.Loader:

我在这里想念什么?

4

5 回答 5

8

上面的所有答案都需要配置 TEMPLATE_DIRS 或 TEMPLATE_LOADERS,这不是必需的和正确的。您只需将您的应用程序放入 INSTALLED_APPS。

例如,如果您的应用程序位于 MyMegaApp(settigs.py 所在的位置)中,换句话说,您的项目结构如下所示

MyMegaApp
   MyMegaApp
      templates
         index.html
      settings.py 
   manage.py

然后你必须像这样添加你的应用程序

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'MyMegaApp'
)

之后,您的index.html模板将在MyMegaApp/templates文件夹中找到

于 2013-03-05T23:59:50.257 回答
3

这里的问题是,在您的设置中,您PROJECT_ROOT评估的是运行manage.py.

您可以为TEMPLATE_DIRS设置执行此操作

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
print PROJECT_ROOT    

现在,../../相对于PROJECT_ROOT. 像这样的东西:

PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))

TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, "templates"),
]
于 2012-09-17T03:13:27.767 回答
2

将您更改PROJECT_ROOT为:

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

并确保您的TEMPLATE_LOADERS变量设置正确。

解释:

abspath给你完整的路径base.py,即/home/some-path/project-folder/settings/base.py

因此,首先dirname给你给定路径的 dir 路径名(上面获得),即/home/some-path/project-folder/settings/

然后,第二个dirname给你给定路径的 dir 路径名(上面获得),即/home/some-path/project-folder/

所以,现在当你加入这条路径时templates,一切都开始正常了。

有关更多信息,请参阅python 文档

于 2012-09-17T05:44:42.957 回答
0

你可以测试如下吗?

我认为您的代码会将 PROJECT_ROOT 设置为 '/some/path/to/settings'

from os.path import dirname, abspath, normpath, join
PROJECT_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(PROJECT_ROOT, 'templates')),
)
于 2012-09-17T05:40:21.117 回答
0

将此包含在设置中

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(SITE_ROOT, 'appname/templates'),

)

将此添加到 urls.py

from django.conf.urls.defaults import *
from  django.views.generic.simple.direct_to_template import direct_to_template
from appname.views import *

调用网址

  urlpatterns = patterns('django.views.generic.simple',
    (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
    (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
  )
于 2012-09-17T06:30:38.160 回答