Django CMS 2.1.0 应用扩展 NoReverseMatch 模板语法错误

0 投票
3 回答
1367 浏览
提问于 2025-04-16 02:28

我正在为Django CMS写一个自定义应用,但在尝试查看后台发布的条目时遇到了以下错误:

在 /admin/cmsplugin_publisher/entry/ 处出现了模板语法错误

在渲染时捕获到 NoReverseMatch:无法找到 'cmsplugin_publisher_entry_detail' 的反向匹配,参数为 '()',关键字参数为 '{'slug': u'test-german'}'。

如果我在主应用的 urls.py 中给这个应用设置一个URL,它就能正常工作,但这样就把应用固定在了一个特定的URL上。我只是想扩展Django CMS,让这个应用可以从它被添加到的任何页面调用。

models.py 中的绝对URL模式

    @models.permalink
    def get_absolute_url(self):
        return ('cmsplugin_publisher_entry_detail', (), {
            'slug': self.slug})

urls/entries.py

from django.conf.urls.defaults import *
from cmsplugin_publisher.models import Entry
from cmsplugin_publisher.settings import PAGINATION, ALLOW_EMPTY, ALLOW_FUTURE

entry_conf_list = {'queryset': Entry.published.all(), 'paginate_by': PAGINATION,}

entry_conf = {'queryset': Entry.published.all(),
    'date_field': 'creation_date',
    'allow_empty': ALLOW_EMPTY,
    'allow_future': ALLOW_FUTURE,
}

entry_conf_detail = entry_conf.copy()
del entry_conf_detail['allow_empty']
del entry_conf_detail['allow_future']
del entry_conf_detail['date_field']
entry_conf_detail['queryset'] = Entry.objects.all()

urlpatterns = patterns('cmsplugin_publisher.views.entries',
    url(r'^$', 'entry_index', entry_conf_list,
        name='cmsplugin_publisher_entry_archive_index'),
    url(r'^(?P<page>[0-9]+)/$', 'entry_index', entry_conf_list,
        name='cmsplugin_publisher_entry_archive_index_paginated'),
)

urlpatterns += patterns('django.views.generic.list_detail',
    url(r'^(?P<slug>[-\w]+)/$', 'object_detail', entry_conf_detail,
        name='cmsplugin_publisher_entry_detail'),
)

views/entries.py

from django.views.generic.list_detail import object_list
from cmsplugin_publisher.models import Entry
from cmsplugin_publisher.views.decorators import update_queryset

entry_index = update_queryset(object_list, Entry.published.all)

views/decorators.py

def update_queryset(view, queryset, queryset_parameter='queryset'):
    '''Decorator around views based on a queryset passed in parameter which will force the update despite cache
    Related to issue http://code.djangoproject.com/ticket/8378'''

    def wrap(*args, **kwargs):
        '''Regenerate the queryset before passing it to the view.'''
        kwargs[queryset_parameter] = queryset()
        return view(*args, **kwargs)
    return wrap

关于应用与Django CMS集成的说明可以在这里找到:http://github.com/divio/django-cms/blob/master/cms/docs/app_integration.txt

看起来问题可能是因为我没有正确返回 RequestContext,因为我在应用中混用了通用视图和自定义视图。

CMS应用扩展的py文件:

cms_app.py

from django.utils.translation import ugettext_lazy as _

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from cmsplugin_publisher.settings import APP_MENUS

class PublisherApp(CMSApp):
    name = _('Publisher App Hook')
    urls = ['cmsplugin_publisher.urls']

apphook_pool.register(PublisherApp)

任何建议都非常感谢,这个问题让我很头疼!

3 个回答

0

我建议你再确认一下,urls/entries.py 这个文件是不是在某个地方被引入了。如果没有引入的话,它就无法找到反向匹配的内容。

0

更新:

好的,我觉得你的错误是出在 get_absolute_url 这个地方:

@models.permalink
def get_absolute_url(self):
    return ('cmsplugin_publisher_entry_detail', (), {'slug': self.slug})

我怀疑是因为这个最终调用了 object_detail,而这个函数需要一个位置参数 queryset(可以查看 django/views/generic/list_detail.py)。你可以试着把它改成类似下面这样的:

    return ('cmsplugin_publisher_entry_detail', [Entry.objects.all(),], {'slug': self.slug})
1

看起来这是Django-CMS 2.1.0beta3版本中的一个URL配置解析器的错误。这个问题在开发版本中已经被修复了。这个错误只会在从一个应用中包含其他URL配置时出现。

撰写回答