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?

Community
  • 1
  • 1
craftApprentice
  • 2,697
  • 17
  • 58
  • 86

6 Answers6

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.

wombatonfire
  • 4,585
  • 28
  • 36
  • 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! – craftApprentice Jun 17 '12 at 14:23
9

This solved it for me:

mkdir templates
echo "hello world" > templates/index.html
jcarballo
  • 27,395
  • 3
  • 28
  • 28
  • 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 '16 at 05:30
5
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),autoescape = True)
4b0
  • 21,981
  • 30
  • 95
  • 142
  • just add a folder called templates and put index.html there – Tshoarelo Moleke Apr 21 '20 at 03: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" – SherylHohman Apr 22 '20 at 00:50
4

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!

craftApprentice
  • 2,697
  • 17
  • 58
  • 86
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

Rich Walsh
  • 986
  • 6
  • 3
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')
AbhiGupta
  • 474
  • 1
  • 6
  • 14