Djangocms中的自定义视图?

yur*_*i2k 4 python django django-cms

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

url(r'^', 'myapp.views.slideshow_info'),
Run Code Online (Sandbox Code Playgroud)

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

目前我的应用程序的观点是:

from myapp.models import model1, model2

def slideshow_info(request):
    return render_to_response('index.html', {'slideshow_list' : model1.objects.all()})
Run Code Online (Sandbox Code Playgroud)

非常感谢

Bra*_*don 6

您可以将自定义应用程序实例挂钩到任何Django-CMS页面.下面是关于如何做到这一点的文档:http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks不应该需要改变基本URL模式,以更具体地说-route /到你的看法.

在自定义app-hooks可用之前,我将完成您尝试使用模板标记所做的事情.

希望能帮到你.

跟进

好的,在最近完成的网站中,我不得不挂钩一个名为"portfolio"的应用程序,以在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
Run Code Online (Sandbox Code Playgroud)

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

希望能让你前进.