0

我正在扩展 Horizo​​n 以包含一个自定义应用程序。在那个应用程序中,我有一个 DataTable:

class WorkloadsTable(tables.DataTable):
    name = tables.Column("name", verbose_name=_("Name"))
    description = tables.Column("description", verbose_name=_("Description"))
    image = tables.Column("image", verbose_name=_("Image"))
    flavor = tables.Column("flavor", verbose_name=_("Flavor"))

    class Meta:
        name = "workloads_table"
        verbose_name = _("Workloads Table")
        table_actions = (CreateNewWorkload, DeleteWorkload)
        row_actions = (UpdateWorkload, DeleteWorkload)

它有一个 LinkAction:

class UpdateWorkload(tables.LinkAction):
    name = "update"
    verbose_name = _("Edit Workload")
    url = "horizon:mydashboard:workloads_panel:update"
    classes = ("ajax-modal",)
    icon = "pencil"

    def get_link_url(self, datum):
        base_url = super(UpdateWorkload, self).get_link_url(datum)
        workload_id = self.table.get_object_id(datum)
        reversed = urlresolvers.reverse(self.url, args=[workload_id])
        print reversed
        return urlresolvers.reverse(self.url, args=[workload_id])

这个 LinkAction 指向我的 urls.py 中的一个路径:

WORKLOADS = r'(?P<workload_id>[^/]+)/%s$'

urlpatterns = patterns('',
    url(r'^create/$', views.CreateView.as_view(), name='create'),
    url(WORKLOADS % 'update', views.UpdateView.as_view(), name='update'),
    url(r'^$', views.IndexView.as_view(), name='index'),
)

问题是:当我输入以下网址时:

http://localhost:9000/mydashboard/workloads_panel/3/update

I get:  NoReverseMatch: Reverse for 'update' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'mydashboard/workloads_panel/(?P<workload_id>[^/]+)/update$']

我哪里错了?

4

2 回答 2

2

我想您的问题的解决方案在于 _update.html 您必须在其中包含 URL,

{% block form_action %}
{% url 'horizon:mydashboard:workloads_panel:update' workload.id  %}
{% endblock %}

而workload.id 是您在views.py 的get_context_data 函数中获得的对象ID:

def get_context_data(self, **kwargs):

    context = super(## the class name ##, self).get_context_data(**kwargs)
    try:
        context['workload'] = ##your API call ##(self.request,
                                                self.kwargs['id'])
    except Exception:
        exceptions.handle(self.request)
    return context

可能这将帮助您解决错误。

于 2015-03-05T10:37:03.583 回答
0

那么现在答案很简单。此反向调用没有匹配的 URL:

{% url 'horizon:mydashboard:workloads_panel:update' %}

命名的updateURL 模式需要一个workload_id与您的正则表达式匹配的有效参数[^/]+

于 2015-01-22T21:41:31.647 回答