Python CKAN:未将上下文传递到新模板
在我的CKAN扩展中,我将向用户仪表板添加一个新选项卡。我按照中所述的程序进行操作,似乎有效。在新页面的控制器中,我设置模板变量的方式与在仪表板上为其他页面设置模板变量的方式相同。但是,当我单击选项卡并加载新页面时,我得到了Python CKAN:未将上下文传递到新模板,python,ckan,Python,Ckan,在我的CKAN扩展中,我将向用户仪表板添加一个新选项卡。我按照中所述的程序进行操作,似乎有效。在新页面的控制器中,我设置模板变量的方式与在仪表板上为其他页面设置模板变量的方式相同。但是,当我单击选项卡并加载新页面时,我得到了UndefinedError:“user_dict”是未定义的。怎么了 以下是我的扩展名/templates/user/dashboard.html的相关部分,我在其中添加了选项卡: {% ckan_extends %} {% block page_header %}
UndefinedError:“user_dict”是未定义的。怎么了
以下是我的扩展名/templates/user/dashboard.html的相关部分,我在其中添加了选项卡:
{% ckan_extends %}
{% block page_header %}
<header class="module-content page-header hug">
<div class="content_action">
{% link_for _('Edit settings'), named_route='user.edit', id=user.name, class_='btn btn-default', icon='cog' %}
</div>
<ul class="nav nav-tabs">
{{ h.build_nav_icon('dashboard.index', _('News feed')) }}
{{ h.build_nav_icon('dashboard.datasets', _('My Datasets')) }}
{{ h.build_nav_icon('dashboard.organizations', _('My Organizations')) }}
{{ h.build_nav_icon('dashboard.groups', _('My Groups')) }}
{{ h.build_nav_icon('my_extension_dashboard_owned_datasets', _('My Owned Datasets')) }}
</ul>
</header>
{% endblock %}
插件类定义的相关部分:
class MyThemePlugin(plugins.SingletonPlugin, DefaultTranslation):
plugins.implements(plugins.IRoutes, inherit=True)
# IRoutes
def before_map(self, map):
with SubMapper(
map, controller="ckanext.my_extension.controller:MyUserController"
) as m:
m.connect(
"my_extension_dashboard_owned_datasets",
"/dashboard/owned_datasets",
action="dashboard_owned_datasets",
)
return map
这是新的控制器,在my_extension/controller.py中:
{% extends "user/dashboard_datasets.html" %}
# encoding: utf-8
import logging
import ckan.lib.base as base
from ckan.common import c
from ckan.controllers.user import UserController
log = logging.getLogger(__name__)
render = base.render
class MyUserController(UserController):
def dashboard_owned_datasets(self):
context = {"for_view": True, "user": c.user, "auth_user_obj": c.userobj}
data_dict = {"user_obj": c.userobj, "include_datasets": True}
self._setup_template_variables(context, data_dict)
log.critical(c.user_dict)
return render(
"user/dashboard_owned_datasets.html"
)
如您所见,我使用UserController
的\u setup\u template\u variables
方法,该方法用于该控制器中的所有其他仪表板操作。该方法设置c.user\u dict
,以及其他内容:
def _setup_template_variables(self, context, data_dict):
c.is_sysadmin = authz.is_sysadmin(c.user)
try:
user_dict = get_action('user_show')(context, data_dict)
except NotFound:
h.flash_error(_('Not authorized to see this page'))
h.redirect_to(controller='user', action='login')
except NotAuthorized:
abort(403, _('Not authorized to see this page'))
c.user_dict = user_dict
c.is_myself = user_dict['name'] == c.user
c.about_formatted = h.render_markdown(user_dict['about'])
在控制器中设置后,我正在记录c.user_dict
,我看到了我希望在那里看到的所有数据
但是,当我单击选项卡并加载时,会出现错误UndefinedError:“user_dict”是undefined
。我遗漏了什么?答案是,IRoutes
插件接口不再适用于我想要使用的功能,因为CKAN从挂架迁移到了烧瓶。应改用IBlueprint
界面。有关蓝图以及如何将扩展从使用IRoutes
转换为IBlueprint
的更多信息,请参阅