在Django项目中放置模板的最佳位置是什么?

在Django项目中放置模板的最佳位置是什么?

从Django书中, 第4章 :

如果你不能想到一个明显的放置模板的地方,我们build议在你的Django项目中创build一个templates目录(例如,如果你一直关注我们的例子,在第二章创build的mysite目录中)。

这正是我所做的,为我工作的很好。

我的目录结构如下所示:

/media所有我的CSS / JS /图像等
/templates为我的模板
/projectname项目名称为主要项目代码(即Python代码)

放置在<PROJECT>/<APP>/templates/<APP>/template.html用于特定于应用程序的模板,以帮助在其他地方使应用程序可重用。

对于一般的“全局”模板,我把它们放在<PROJECT>/templates/template.html

在Dominic和dlrust之后,

我们使用setuptools源代码发布(sdist)来打包我们的django项目和应用程序,以部署在我们不同的环境中。

我们发现模板和静态文件需要在django应用程序目录下,以便它们可以被setuptools打包。

例如,我们的模板和静态path如下所示:

 PROJECT/APP/templates/APP/template.html PROJECT/APP/static/APP/my.js 

为此,需要修改MANIFEST.in(请参阅http://docs.python.org/distutils/sourcedist.html#the-manifest-in-template

MANIFEST.in的一个例子:

 include setup.py recursive-include PROJECT *.txt *.html *.js recursive-include PROJECT *.css *.js *.png *.gif *.bmp *.ico *.jpg *.jpeg 

另外,您需要在您的django设置文件中确认app_directories加载器位于TEMPLATE_LOADERS中。 我认为它在django 1.4中是默认的。

django设置模板加载器的一个例子:

 # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) 

以防万一你想知道为什么我们使用sdists而不是只是应对rsync文件; 这是我们的configurationpipe理工作stream程的一部分,我们有一个单独的生成压缩包,并将PIP部署到testing,验收和生产环境中。

您也可以考虑使用django-dbtemplates在数据库中创build模板。 它也被设置为caching,django-reversion应用程序可以帮助您保留旧版本的模板。

它工作得很好,但是我更喜欢在文件系统的导入/同步方面有更多的灵活性。

DJANGO 1.11

添加manage.py所在的模板文件夹,这是您的基本目录。 更改您的settings.py中的TEMPLATES DIRS

 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, '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', ], }, }, 

]

现在通过使用该代码来使用该模板,

 def home(request): return render(request,"index.html",{}) 

在views.py中。 这对django 1.11来说是完全正常的

这在项目层面更是个人select。 如果您正在讨论需要可插入的应用程序,那么应用程序中的模板目录就是默认的地方。 但是在整个项目中,最适合你的是什么。

我了解TEMPLATE_DIRS需要一个绝对path。 而且我不喜欢我的代码中的绝对path。 所以这对我来说很好,在settings.py

 import os TEMPLATE_DIRS = ( os.path.join(os.path.dirname(os.path.realpath(__file__)), "../APPNAME/templates") ) 

Django 1.10

TEMPLATE_DIRS已被弃用。

现在我们需要使用TEMPLATE ,像这样在Django 1.8中引入 :

 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { # ... some options here ... }, }, ] 

一旦您定义了模板,您可以安全地删除ALLOWED_INCLUDE_ROOTS,TEMPLATE_CONTEXT_PROCESSORS,TEMPLATE_DEBUG,TEMPLATE_DIRS,TEMPLATE_LOADERS和TEMPLATE_STRING_IF_INVALID。

关于最好的位置,Django寻找这样的模板:

  • DIRS根据search顺序定义引擎应该查找模板源文件的目录列表。
  • APP_DIRS告诉引擎是否应该在已安装的应用程序中查找模板。 每个后端都为其模板应存储在其中的应用程序内的子目录定义一个常规名称。

更多信息: https : //docs.djangoproject.com/en/1.10/topics/templates/#configuration

以前的解决scheme不适用于我的情况。 我用了:

 TEMPLATE_DIRS = [ os.path.join(os.path.dirname(os.path.realpath(__file__)),"../myapp/templates") ]