django: can we do loader.get_template('my_template.txt')?

寵の児 提交于 2020-01-23 07:49:09

问题


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')

Can we load txt files using django template loader? how?

thanks.


回答1:


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.

Check the following:

  1. 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.

  2. 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.

  3. As @Seth said double check your TEMPLATE_DIRS setting. The template should be inside one the directories in this list.




回答2:


I would leave this for some one else to answer as I am not very comfortable with Django.

How ever, if you are interested in templates and plain text processing, why don't you look at slew of other products available within python.

  • https://stackoverflow.com/questions/98245/what-is-your-single-favorite-python-templating-engine
  • http://wiki.python.org/moin/Templating


来源:https://stackoverflow.com/questions/3817926/django-can-we-do-loader-get-templatemy-template-txt

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