django:我们可以做loader.get_template('my_template.txt')吗? [英] django: can we do loader.get_template('my_template.txt')?
问题描述
我想使用django模板来处理纯文本文件,并尝试这样做:
I want to use django template to process plain text file, and tried this:
from django.template import loader, Context
t = loader.get_template('my_template.txt')
然而,它适用于这个:
however, it works for this:
from django.template import loader, Context
t = loader.get_template('my_template.html')
我们可以使用django模板加载器加载txt文件吗?如何?
Can we load txt files using django template loader? how?
谢谢。
推荐答案
不明白为什么这不行。 Django不关心文件的扩展名。你可以很好地加载 my_template.foo
。
As @Seth commented I don't see any reason why this shouldn't work. Django doesn't care about the extension of the file. You can very well load my_template.foo
.
检查以下内容:
-
该文件确实存在于它应该是。如果它在一个子目录中,那么你必须使用
loader.get_template('<子目录> /my_template.txt')
其中的子目录是目录的名称。
The file is indeed present where it should be. If it is in a subdirectory then you'll have to use
loader.get_template('<subdirectory>/my_template.txt')
where subdirectory is the name of the directory.
检查您是否有应用程序名称。将应用的所有模板定位到具有应用名称的目录中。
Check if you have an app name. It is common to locate all templates for an app in a directory with the app's name.
由于@Seth表示双重检查您的 TEMPLATE_DIRS
设置。该模板应位于此列表中的目录之一。
As @Seth said double check your TEMPLATE_DIRS
setting. The template should be inside one the directories in this list.
这篇关于django:我们可以做loader.get_template('my_template.txt')吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!