【问题标题】:Django-cms Apphook url doesn't loadDjango-cms Apphook url 未加载
【发布时间】:2013-05-09 15:44:27
【问题描述】:

我有一个 django-cms 项目,其中包含一个名为 core 的应用程序。在核心内部,我创建了一个文件“cms_app.py”,如下所示:

# -*- coding: utf8 -*-
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _

class CoreApphook(CMSApp):
    name = _(u"Core Apphook")
    urls = ["core.urls"]

apphook_pool.register(CoreApphook)

在我的 core/urls.py 我有以下代码:

# -*- coding: utf8 -*-
from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',

        # URLS refrentes ao apphook CoreApphook
        url(r'^$', 'noticia.views.ultimas_noticias'),
        url(r'^noticias/$', 'noticia.views.ultimas_noticias'),
        url(r'^noticias/(?P<categoria>[\w\d-]+)/$', 'noticia.views.noticias_categoria'),
        url(r'^noticias/(?P<categoria>[\w\d-]+)/(?P<pagina>\d+)/$', 'noticia.views.noticias_categoria_paginated'),
        url(r'^noticias/(?P<categoria>[\w\d-]+)/(?P<subcategoria>[\w\d-]+)/(?P<titulo>[\w\d-]+)/$', 'noticia.views.noticia'),
        url(r'^paginacao/noticias/$', 'noticia.views.noticias_categoria_paginated'),
    )

我正在尝试到达这个视图:

url(r'^noticias/(?P<categoria>[\w\d-]+)/(?P<subcategoria>[\w\d-]+)/(?P<titulo>[\w\d-]+)/$', 'noticia.views.noticia'),

通过使用这个网址:

http://127.0.0.1:8000/noticias/filmes/acao/lol-e-poka-zuera/

但是 Apphook 没有加载文件 urls.py。我已经在“Noticias”和“Noticias”的每个子页面中设置了 Apphook 字段。奇怪的是,我在另一个完美运行的项目中具有相同的结构。显然我已经将应用程序“核心”设置为 INSTALLED_APPS。我什至无法想象是什么导致了这个问题。我在 core/urls.py 中使用了断点,但 Apphook 没有调用它。

【问题讨论】:

    标签: python django url django-cms


    【解决方案1】:
    urlpatterns = patterns('',
    
        # URLS refrentes ao apphook CoreApphook
        url(r'^$', 'noticia.views.ultimas_noticias', name='app_ultimas_noticias'),
        url(r'^noticias/$', 'noticia.views.ultimas_noticias', name='app_ultimas_noticias1'),
    )
    

    【讨论】:

    • 我发现了问题。出现这种情况的原因是新版本的django-cms要求页面修改时要发布两次。
    • @Mauricio 感谢您对解决方案的评论。您可能应该添加答案并接受它,因为我遇到了同样的问题。
    【解决方案2】:

    您是否重新启动了服务器? (即使您使用 manage.py 运行服务器也必须重新启动它)

    进一步说明,您必须在视图中使用 RequestContext。 https://django.readthedocs.org/en/latest/ref/templates/api.html#subclassing-context-requestcontext

    我刚刚遇到了问题,以下帮助:

    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def some_view(request):
        # ...
        return render_to_response('my_template.html',
               my_data_dictionary,
               context_instance=RequestContext(request))
    

    编辑:也许我错过了理解这个问题。问题是您的 apphook 没有得到任何输出还是无法链接到它?

    如果是第二种情况,也许django-cms: urls used by apphooks don't work with reverse() or {% url %} 可以帮助你。

    EDIT 2刚刚发现当前的django-cms没有cms.middleware.multilingual.MultilingualURLMiddleware了。

    【讨论】:

      【解决方案3】:

      正如 OP 评论的那样,这是一个设计限制。如果页面尚未发布,Django CMS 将不会加载 apphooked 视图。
      它仍然像那样工作(当前版本是 3.3.0),所以要使 apphook 工作,您需要发布页面。
      github上有一个相关问题:https://github.com/divio/django-cms/issues/2605

      【讨论】:

        猜你喜欢
        • 2019-01-14
        • 2012-03-28
        • 2011-07-26
        • 2012-03-30
        • 2015-09-04
        • 2011-07-26
        • 1970-01-01
        • 2014-03-20
        • 2017-11-15
        相关资源
        最近更新 更多