18

I'm trying to build my first GAE app with jinja2. After overcoming a dozen small errors, now I'm stuck with this:

Traceback (most recent call last):

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\CG\Documents\udacity\HiMon\main.py", line 31, in get
    template = jinja_environment.get_template('index.html')
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 180, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: index.html

Here my yaml file:

application: himother
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: "2.6"

Here my code:

import os
import webapp2

import jinja2

jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class MainPage(webapp2.RequestHandler):
    def get(self):
        template_values = {
            'name': 'Serendipo',
            'verb': 'extremely happy'
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)

Here my .html template:

<!DOCTYPE html>
<html>
    <head>
        <title>Look Ma, I'm using Jinja!</title>
    </head>
    <body>
        Hi there - I'm {{ name }}, and I {{ verb }} programming!
    </body>
</html>

Despite the error message, I have a folder called "templates" and, within it, created the index.html file:

enter image description here

enter image description here

enter image description here

I also have installed jinja2.

Does anyone have any idea of the cause of this error now?

0

6 Answers 6

15

Try to use

loader=jinja2.FileSystemLoader('templates')

instead of

loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))

It works for me.

1
  • Hi, @v0hkus, thanks for your answer, but the error persists: File "C:\Program Files (x86)\Google\google_appengine\lib\jinja2\jinja2\loaders.py", line 180, in get_source raise TemplateNotFound(template) TemplateNotFound: index.html INFO 2012-06-17 14:20:15,908 dev_appserver.py:2891] "GET / HTTP/1.1" 500 - Thanks again! Jun 17, 2012 at 14:23
9

This solved it for me:

mkdir templates
echo "hello world" > templates/index.html
1
  • For some reason, I actually had to delete the files and then re-add them in order for Flask to detect that the files were there. Must have created them wrong initially
    – sofly
    Jun 27, 2016 at 5:30
5

Well, my error was simple and silly. I have create the file "index.html" the wrong way (here the right way). So, my "index.html" file was indeed a ".text" file (because I just rename it to "index.html" instead of "save as" index.html"). Thanks for the help, guys!

1
  • That link is now broken - is there an update? May 11 at 14:37
5
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)
2
  • just add a folder called templates and put index.html there Apr 21, 2020 at 3:32
  • While this code may work as a solution, it is better to add context, and explain your code. This helps future visitors hone in on what is important to pay attention to in this code, and helps others learn, so they can apply this knowledge to their own code. Or address how/why this code differs from the OP's attempts and contrast why this solution works. This keeps SO quality high, discourages "give me the code" questions, and encourages sharing knowledge & information. Visitors are more likely to pay attention to & read informative posts. High quality Answers are more likely to get "upvotes" Apr 22, 2020 at 0:50
2

Two thoughts based on getting my first GAE effort with Jinja2 to work. First, in your yaml file, you have "-url: ." though I used "-url: /." based on tutorials I saw. However, this may be irrelevant to your issue. Second, I used the guidance on this page for how I established my Jinja2 renderer and had no issue with the template being found in the templates subdirectory of the application directory: http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html#module-webapp2_extras.jinja2

0

I received the same error and tried all answers. Later, I got to know that you first need to configure the environment with the right folder to search the html file and only then jinja2 will be able to locate the file.

Following lines will remove the error:

env = Environment(loader=FileSystemLoader(searchpath='C:\Folder\of\html\file')
template = env.get_template('Name_of_file.html')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.