2013-05-15 38 views
4

我有一個django-cms項目,其中包含一個名爲core的應用程序。內芯我創建如下文件「cms_app.py」:Django-cms Apphook url無法加載

# -*- 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) 

在我的核心/ 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/ 

但是文件urls.py不是由Apphook加載的。我已經在「Noticias」和「Noticias」的每個子頁面中設置了Apphook字段。奇怪的是,我在另一個完美工作的項目中擁有相同的結構。很明顯,我已將應用程序「核心」設置爲INSTALLED_APPS。我甚至無法想象可能會造成這個問題。我在我的core/urls.py中使用了一個斷點,但它並沒有被Apphook調用。

回答

2
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'), 
) 
+0

沒有爲我工作。 – Mauricio

+5

我發現了這個問題。發生這種情況的原因是,新版本的django-cms需要在修改頁面時將頁面發佈兩次。 – Mauricio

+1

@Mauricio感謝您的解決方案評論。您應該添加答案並接受它,因爲我遇到同樣的問題。 – digitaldreamer

0

您是否重新啓動服務器? (即使你使用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 %}可以幫助你。

編輯2剛發現目前的django-cms沒有cms.middleware.multilingual.MultilingualURLMiddleware了。

0

正如OP評論的那樣,這是一個設計限制。如果頁面尚未發佈,Django CMS將不會加載apphooked視圖。
它仍然是這樣工作的(當前版本是3.3.0),所以爲了使apphook工作,你需要發佈頁面。
github上有個問題:https://github.com/divio/django-cms/issues/2605