В своем расширении CKAN я добавляю новую вкладку на панель пользователя. Я следовал процедуре, описанной в этом ответе , и, похоже, это сработало. В контроллере для новой страницы я установил переменные шаблона таким же образом, как они установлены для других страниц на панели инструментов. Когда я нажимаю на вкладку и загружаю новую страницу, я получаю UndefinedError: 'user_dict' is undefined
. Что не так?
Вот соответствующая часть my_extension / 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 %}
Вот новый шаблон на данный момент, my_extension / templates / user / dashboard_owned_datasets. html:
{% extends "user/dashboard_datasets.html" %}
Соответствующая часть определения класса плагина:
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 :
# 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
'_setup_template_variables
, который используется во всех другие действия панели в этом контроллере. Этот метод устанавливает c.user_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
после установки его в контроллере, и я вижу все данные, которые я ожидаю увидеть там.
Но когда я нажимаю на вкладку и загружаю http://localhost: 5000 / dashboard / own_datasets , я получаю ошибку UndefinedError: 'user_dict' is undefined
. Чего мне не хватает?