【问题标题】:Problems configuring gae_mini_profiler correctly正确配置 gae_mini_profiler 的问题
【发布时间】:2015-05-21 20:40:55
【问题描述】:

我无法让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 部分中定义函数时是否指定了错误?

【问题讨论】:

    标签: python google-app-engine jinja2


    【解决方案1】:

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

    import gae_mini_profiler.templatetags
    

    【讨论】:

    • 谢谢,杰弗里!但是在 Jinja2 中似乎也有一些东西,并定义了在模板中使用的函数,这会导致错误。你有什么想法会发生什么吗?
    • 不是 Jinja2,是 gae_mini_profiler。那里的说明基本上是针对 python 2.5 运行时的。 @kamens 在这里解释更多 stackoverflow.com/a/8338910/2295256
    【解决方案2】:

    关于更新

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

    而不是这个:{% profiler_includes %}

    是我最终能够在 python2.7 / jinja2 中使用 gae_mini_profiler 的包含...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-20
      • 2021-11-16
      • 1970-01-01
      • 2019-11-20
      • 2019-05-09
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多