Edgar Navasardyan Edgar Navasardyan - 1 month ago 10
Python Question

Django Jinja2 - AttributeError: 'FileSystemLoader' object has no attribute 'get_source' error

I am using WEasyPrint and Jinja2 to generate a PDF out of a template, and have the following code:

env = Environment(loader=FileSystemLoader(self.app.templates_path))
template = env.get_template("order_scheduler/pdf_order_description.html") # ERROR
html_out = template.render()


The problem is that I am getting
AttributeError: 'FileSystemLoader' object has no attribute 'get_source'
on the second line of the code. Tried to surf the internet, but didn't find any sources on this.

I think, settings file content might be helpful. Find it below:

INSTALLED_APPS = [
#...
'django_jinja',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'supermarket_project.urls'
#ANONYMOUS_USER_ID=-1
TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
"APP_DIRS": True,
"OPTIONS": {
"match_extension": ".jinja",
}
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(BASE_DIR),"static","templates")],
#'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'
],
},
},
]

WSGI_APPLICATION = 'supermarket_project.wsgi.application'
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

STATIC_URL = '/static/'

if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR),"static","static"),
)


The full traceback:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 148, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 146, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 184, in inner
return func(*args, **kwargs)
File "C:\Users\edgar\Desktop\djprj\djprj\vendor\views.py", line 383, in get_pdf
template = env.get_template("pdf_order_description.html")
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
AttributeError: 'FileSystemLoader' object has no attribute 'get_source'


The content of
pdf_order_description.html
is as simple as
<p>Stackoverflow</p>


Any ideas how to fix the error ?

Answer

Not sure why you're doing that stuff with env and FileSystemLoader at all.

Django already has support for Jinja2, and you've configured that in your settings. You should use the standard template loading functionality to get your Jinja template:

html_out = render_to_string('order_scheduler/pdf_order_description.jinja')

Note that you've configured the template loader to expect the extension "jinja" to load Jinja templates, so you should use that for your template.