Djangocms中的自定义视图?

时间:2011-05-11 13:23:17

标签: python django django-cms

我还没有找到一个令人满意的方法:我有一个工作正常的djangocms设置。但我需要将CMS外部表格中的内容添加到我的主页,并在模板上呈现该内容。我可以这样做,但在CMS中编辑urls.py以使用我的观点......

url(r'^', 'myapp.views.slideshow_info'),

...从CMS中排除任何内容。我知道我只是获取自定义视图以适应CMS的观点,但我该如何实现?

目前我的应用程序的观点显示:

from myapp.models import model1, model2

def slideshow_info(request):
    return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})

非常感谢

1 个答案:

答案 0 :(得分:6)

您可以将自定义应用实例挂钩到任何Django-CMS页面。以下是有关如何执行此操作的文档:http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks不应该需要更改基本网址模式以专门重新路由/到您的视图。

在自定义应用程序挂钩可用之前,我将完成您尝试使用模板标记执行的操作。

希望能帮到你。

跟进

好的,在最近完成的网站中,我不得不挂钩一个名为“作品集”的应用程序,在Django-CMS网站的主页上显示图像。

以下是代码的相关部分:

#portfolio/cms_app.py
from django.utils.translation import ugettext_lazy as _

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool

class PortfolioAppHook(CMSApp):
    name = _('Portfolio')
    urls = ['portfolio.urls']

apphook_pool.register(PortfolioAppHook)


#portfolio/urls.py
from django.conf.urls.defaults import *

urlpatterns = patterns('portfolio.views',
    url(r'^(?P<slug>[-\w]+)/$', 'project_detail', name='project_detail'),
    url(r'^$', 'portfolio_index', name='portfolio_index'),
)

#portfolio/views.py
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render

from portfolio.models import Project


def portfolio_index(request):
    project_objects = Project.for_public if request.user.is_anonymous() \
        else Project.objects
    projects = project_objects.all().select_related(depth=1)
    return render('portfolio/index.html',
        {'projects' : projects}, request)


def project_detail(request, slug):
    project = get_object_or_404(Project, slug=slug)
    if not project.public and request.user.is_anonymous():
        return HttpResponseRedirect('/?login=true')
    return render('portfolio/project_detail.html',
        {'project' : project}, request)

#urls.py (base urls)
from django.conf import settings
from django.conf.urls.defaults import *
from django.contrib import admin

from views import login_user, logout_user

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/filebrowser/', include('filebrowser.urls')),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
    (r'^tinymce/', include('tinymce.urls')),
    url(r'^login/$', login_user, name='login_user'),
    url(r'^logout/$', logout_user, name='logout_user'),
    (r'^', include('sorl.thumbnail.urls')),
    (r'^', include('cms.urls')),
)

if settings.SERVE_STATIC_MEDIA:
    urlpatterns += patterns('',
        (r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
    ) + urlpatterns

从这个工作示例中可以看出,我没有更改我的基本URL以适应主页视图,而是通过cms_app.py将我的Portfolio应用程序的URL提供给Django-CMS

希望能让你前进。