Django: is it possible to execute loader.get_template ('my_template.txt')?

I want to use the django template to process a 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 upload txt files using the django template loader? as?

thank.

+3
source share
2 answers

As @Seth noted, I see no reason why this should not work. Django does not care about the file extension. You can download very well my_template.foo.

Check the following:

  • The file is really present where it should be. If it is in a subdirectory, you will need to use loader.get_template('<subdirectory>/my_template.txt')where the subdirectory is the name of the directory.

  • , . .

  • @Seth , TEMPLATE_DIRS. .

+3

Source: https://habr.com/ru/post/1767084/


All Articles