2012-06-14 24 views
10

Tôi đang cố gắng tạo ứng dụng GAE đầu tiên với jinja2. Sau khi vượt qua hàng chục lỗi nhỏ, bây giờ tôi đang mắc kẹt với điều này:Sự cố với Jinja2: TemplateNotFound: index.html

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 

Ở đây tập tin yaml tôi:

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" 

Ở đây mã của tôi:

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) 

đây mẫu .html của tôi :

<!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> 

Mặc dù er nhắn ROR, tôi có một thư mục có tên "mẫu" và, bên trong nó, tạo ra các tập tin index.html: jinja2

enter image description here

enter image description here

enter image description here

Tôi cũng đã cài đặt.

Có ai có bất kỳ ý tưởng nào về nguyên nhân của lỗi này ngay bây giờ không?

+0

Hey, Làm thế nào để tải một mẫu từ "mẫu/subfolder1"? Bạn có thể vui lòng giúp tôi không ? –

Trả lời

1

Vâng, lỗi của tôi rất đơn giản và ngớ ngẩn. Tôi đã tạo tệp "index.html" sai cách (here đúng cách). Vì vậy, tệp "index.html" của tôi thực sự là tệp ".text" (vì tôi chỉ đổi tên nó thành "index.html" thay vì "save as" index.html"). Cảm ơn sự giúp đỡ, các bạn!

2

Hai ý tưởng dựa trên nỗ lực GAE đầu tiên của tôi với Jinja2 để hoạt động. Đầu tiên, trong tệp yaml của bạn, bạn có "-url:. " mặc dù tôi đã sử dụng "-url: /." dựa trên hướng dẫn mà tôi đã thấy. Tuy nhiên, điều này có thể không liên quan đến vấn đề của bạn. Thứ hai, tôi đã sử dụng các hướng dẫn trên trang này để làm thế nào tôi thành lập renderer Jinja2 của tôi và không có vấn đề với các mẫu được tìm thấy trong các mẫu thư mục con của thư mục ứng dụng: http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html#module-webapp2_extras.jinja2

12

Cố gắng sử dụng

loader=jinja2.FileSystemLoader('templates') 

thay vì

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

Nó phù hợp với tôi.

+0

Xin chào, @ v0hkus, cảm ơn câu trả lời của bạn, nhưng lỗi vẫn tồn tại: Tệp "C: \ Program Files (x86) \ Google \ google_appengine \ lib \ jinja2 \ jinja2 \ loaders.py", dòng 180, trong get_source nâng TemplateNotFound (mẫu) MẫuNotFound: index.html THÔNG TIN 2012-06-17 14: 20: 15,908 dev_appserver.py:2891] "GET/HTTP/1.1" 500 - Cảm ơn bạn lần nữa! – craftApprentice

+1

Sau quá nhiều googling, cuối cùng tôi đã nhận nó làm việc .... Cảm ơn. –

+3

Làm cách nào để tải mẫu từ "mẫu/thư mục con1"? –

6

này giải quyết nó cho tôi:

mkdir templates 
echo "hello world" > templates/index.html 
+0

Vì lý do nào đó, tôi thực sự phải xóa các tệp và sau đó thêm lại chúng để Flask phát hiện ra rằng các tệp đã có. Phải tạo ra chúng sai ban đầu – sofly