Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 543
john Avatar asked Nov 27 '22 19:11

john


1 Answers

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.

like image 145
Manoj Govindan Avatar answered Dec 10 '22 03:12

Manoj Govindan