正确配置 gae_mini_profiler 的问题

Problems configuring gae_mini_profiler correctly

我在让 https://github.com/Khan/gae_mini_profiler 在我的应用程序中正常工作时遇到问题。

我已仔细按照说明操作,这是我的应用程序(删除了不必要的内容。)

import webapp2
from webapp2_extras import jinja2
import appengine_config
import handlers
import gae_mini_profiler.profiler

class MainHandler(handlers.BaseRequestHandler):
    def get(self):
        user = separate_file.get_user_and_logins(self)
        templateid = 'home.html'
        context = {
            'user': user,
        }
        self.render(templateid, context)

app_config = {
    'webapp2_extras.jinja2.default_config': {
        'globals': {
            'profiler_includes': gae_mini_profiler.templatetags.profiler_includes,
         },
     },
}

app = webapp2.WSGIApplication([
  ('/', MainHandler)], config=app_config, debug=True)
app = gae_mini_profiler.profiler.ProfilerWSGIMiddleware(app)

我收到这个错误:

ERROR    2015-05-21 20:23:25,460 wsgi.py:263] 
Traceback (most recent call last):
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
  File "/Users/jedc/mypathtomyapp/main.py", line 33, in <module>
'profiler_includes': gae_mini_profiler.templatetags.profiler_includes,
AttributeError: 'module' object has no attribute 'templatetags'

这可能是因为我在主应用程序中使用的是 webapp2 而不是 webapp?或者我应该以不同的方式导入 gae_mini_profiler 吗?想不通了

更新

在专门导入 gae_mini_profiler.templatetags 之后,我现在遇到了一个新错误。

  File "/Users/jedc/Dropbox (Personal)/code/seeddb-devbranch/seeddbapp/main.py", line 22, in get
self.render(templateid, context)
  File "/Users/jedc/Dropbox (Personal)/code/seeddb-devbranch/seeddbapp/handlers.py", line 64, in render
self.response.write(self.jinja2.render_template(template_name, **values))
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
  File "templates/base.html", line 118, in template
    {% profiler_includes %}
TemplateSyntaxError: Encountered unknown tag 'profiler_includes'.

并且 base.html 的末尾有:

  </div>
  {% profiler_includes %}
  </body>
</html>

现在如果我改变:

{% profiler_includes %}

至:

{{ profiler_includes }}

它清除了错误,但实际上似乎并没有触发该功能。

不是 Jinja2 专家,但是我在 webapp2_extras.jinja2.default_config 部分定义函数时是否指定了错误的内容?

您缺少导入语句。您还应该明确导入 gae_mini_profiler.templatetags 因为您正在使用它。

import gae_mini_profiler.templatetags

关于更新

这个:{{ profiler_includes() | safe }}

而不是这个:{% profiler_includes %}

是我最终能够在 python2.7 / jinja2...

中使用 gae_mini_profiler 的方式