如何将django-fluent-contents实现到Mezzanine现有项目

时间:2015-12-09 09:49:18

标签: project mezzanine

我现有的Mezzanine项目包含现有页面。可以在没有流畅页面功能的情况下实现AdminPage流畅内容功能吗?只想创建Mezzanine Page,但它的内容流畅。这有可能实现吗?任何人都可以举例说明如何将它实现到Mezzanine AdminPage。

1 个答案:

答案 0 :(得分:2)

由于没有人遇到这个问题,我已经弄清楚并成功地将Fluent内容实施到现有的Mezzanine项目中。 这很简单,但调查需要编辑夹层cms的核心资源。所有输出解决方案都是简单的应用程序,可以在管理员或客户端扩展Mezzanine页面。

DIFFICULTY :中/专家)

<强> SOLUTION:

(对于这个例子,我使用了名为&#34的app; cms_coremodul&#34;) PS:它是用ver制作的。 Python 3.4与虚拟环境。

冥想设置和安装:

-Mezzanine 4.0.1

- 使用所需插件安装所需内容的流畅内容 (关注fluent-contents docs)。

pip install django-fluent-contents

- 你也可以选择安装强大的wysiwyg CKEditor。

pip install django-ckeditor

- 完成所有安装后,我们可以设置settings.py并迁移所有内容。

settings.py

-fluent-contents必须高于您的应用程序,低于Mezzanine应用程序。

INSTALLED_APPS = (
    ...
    "fluent_contents",
    "django_wysiwyg",
    "ckeditor",
    # all working fluent-contents plugins
    'fluent_contents.plugins.text',                # requires django-wysiwyg
    'fluent_contents.plugins.code',                # requires pygments
    'fluent_contents.plugins.gist',
    'fluent_contents.plugins.iframe',
    'fluent_contents.plugins.markup',
    'fluent_contents.plugins.rawhtml',
    'fluent_contents.plugins.picture',
    'fluent_contents.plugins.oembeditem',
    'fluent_contents.plugins.sharedcontent',
    'fluent_contents.plugins.googledocsviewer',
    ...
    'here_will_be_your_app',
)

- 设置django-ckeditor:

settings.py

# CORE MODUL DEFAULT WYSIWYG EDITOR SETUP
RICHTEXT_WIDGET_CLASS = "ckeditor.widgets.CKEditorWidget"
RICHTEXT_FILTER_LEVEL = 3
DJANGO_WYSIWYG_FLAVOR = "ckeditor"

# CKEditor config
CKEDITOR_CONFIGS = {
    'awesome_ckeditor': {
        'toolbar': 'Full',
    },
    'default': {
        'toolbar': 'Standard',
        'width': '100%',
    },         
}

- 在设置完fluent内容的settings.py后,我们可以将所有内容迁移出来:

python manage.py migrate

- 如果有关于fluent-contents依赖项的任何错误/错误,请安装该依赖项并再次迁​​移。

为FLUENT-CONTENTS创建新应用程序:

  • 在Mezzanine项目中创建一个新应用程序(与Django相同):

    python manage.py startapp nameofyourapp

models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from mezzanine.pages.models import Page
from django.utils.translation import ugettext_lazy as _
from fluent_contents.models import PlaceholderRelation, ContentItemRelation
from mezzanine.core.fields import FileField
from . import appconfig

class CoreModulPage(Page):

    template_name = models.CharField("Template choice", max_length=255, choices=appconfig.TEMPLATE_CHOICES, default=appconfig.COREMODUL_DEFAULT_TEMPLATE)

    # Accessing the data of django-fluent-contents
    placeholder_set = PlaceholderRelation()
    contentitem_set = ContentItemRelation()

    class Meta:
        verbose_name = _("Core page")
        verbose_name_plural = _("Core pages")

admin.py

from django.contrib import admin
from django.http.response import HttpResponse
from mezzanine.pages.admin import PageAdmin

import json

# CORE MODUL IMPORT
from fluent_contents.admin import PlaceholderEditorAdmin
from fluent_contents.analyzer import get_template_placeholder_data
from django.template.loader import get_template
from .models import CoreModulPage
from . import appconfig
from fluent_contents.admin.placeholdereditor import PlaceholderEditorInline

class CoreModulAdmin(PlaceholderEditorAdmin, PageAdmin):

    #################################
    #CORE MODUL - PAGE LOGIC
    #################################
    corepage = CoreModulPage.objects.all()
    # CORE FLUENT-CONTENTS
    # This is where the magic happens.
    # Tell the base class which tabs to create
    def get_placeholder_data(self, request, obj):
        # Tell the base class which tabs to create
        template = self.get_page_template(obj)
        return get_template_placeholder_data(template)

    def get_page_template(self, obj):
        # Simple example that uses the template selected for the page.
        if not obj:
            return get_template(appconfig.COREMODUL_DEFAULT_TEMPLATE)
        else:
            return get_template(obj.template_name or appconfig.COREMODUL_DEFAULT_TEMPLATE)

    # Allow template layout changes in the client,
    # showing more power of the JavaScript engine.

    # THIS LINES ARE OPTIONAL
    # It sets your own path to admin templates and static of fluent-contents
    #
    # START OPTIONAL LINES
    # this "PlaceholderEditorInline.template" is in templates folder of your app
    PlaceholderEditorInline.template = "cms_plugins/cms_coremodul/admin/placeholder/inline_tabs.html"
    # this "PlaceholderEditorInline.Media.js"
    # and "PlaceholderEditorInline.Media.css" is in static folder of your app
    PlaceholderEditorInline.Media.js = (
            'cms_plugins/cms_coremodul/admin/js/jquery.cookie.js',
            'cms_plugins/cms_coremodul/admin/js/cp_admin.js',
            'cms_plugins/cms_coremodul/admin/js/cp_data.js',
            'cms_plugins/cms_coremodul/admin/js/cp_tabs.js',
            'cms_plugins/cms_coremodul/admin/js/cp_plugins.js',
            'cms_plugins/cms_coremodul/admin/js/cp_widgets.js',
            'cms_plugins/cms_coremodul/admin/js/fluent_contents.js',
        )

    PlaceholderEditorInline.Media.css = {
            'screen': (
                'cms_plugins/cms_coremodul/admin/css/cp_admin.css',
            ),
        }

    PlaceholderEditorInline.extend = False   # No need for the standard 'admin/js/inlines.min.js' here.
    #
    # END OPTIONAL LINES

    # template to change rendering template for contents (combobox in page to choose desired template to render)
    change_form_template = "cms_plugins/cms_coremodul/admin/page/change_form.html"

    class Media:
        js = (
            'cms_plugins/cms_coremodul/admin/js/coremodul_layouts.js',
        )



    def get_layout_view(self, request):
        """
        Return the metadata about a layout
        """
        template_name = request.GET['name']

        # Check if template is allowed, avoid parsing random templates
        templates = dict(appconfig.TEMPLATE_CHOICES)
        if not templates.has_key(template_name):
            jsondata = {'success': False, 'error': 'Template was not found!'}
            status = 404
        else:
            # Extract placeholders from the template, and pass to the client.
            template = get_template(template_name)
            placeholders = get_template_placeholder_data(template)

            jsondata = {
                'placeholders': [p.as_dict() for p in placeholders],
            }
            status = 200

        jsonstr = json.dumps(jsondata)
        return HttpResponse(jsonstr, content_type='application/json', status=status)


admin.site.register(CoreModulPage, CoreModulAdmin)

appconfig.py

- 您必须在应用中创建新的appconfig.py文件。

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


TEMPLATE_CHOICES = getattr(settings, "TEMPLATE_CHOICES", ())
COREMODUL_DEFAULT_TEMPLATE = getattr(settings, "COREMODUL_DEFAULT_TEMPLATE", TEMPLATE_CHOICES[0][0] if TEMPLATE_CHOICES else None)


if not TEMPLATE_CHOICES:
    raise ImproperlyConfigured("Value of variable 'TEMPLATE_CHOICES' is not set!")

if not COREMODUL_DEFAULT_TEMPLATE:
    raise ImproperlyConfigured("Value of variable 'COREMODUL_DEFAULT_TEMPLATE' is not set!")

settings.py : - 此行添加到Mezzanine项目的settings.py。

# CORE MODUL TEMPLATE LIST
TEMPLATE_CHOICES = (
    ("pages/coremodulpage.html", "CoreModulPage"),
    ("pages/coremodulpagetwo.html", "CoreModulPage2"),
)

# CORE MODUL default template setup (if drop-down not exist in admin interface)
COREMODUL_DEFAULT_TEMPLATE = TEMPLATE_CHOICES[0][0]

- 将您的应用添加到INSTALLED_APPS(将您的应用添加到INSTALLED_APPS)。

INSTALLED_APPS = (
    ...
    "yourappname_with_fluentcontents",
)

为您的应用内容创建模板:

-template with one placeholder:

<强> coremodulpage.html:

{% extends "pages/page.html" %}
{% load mezzanine_tags fluent_contents_tags %}

{% block main %}{{ block.super }}
{% page_placeholder page.coremodulpage "main" role='m' %}
{% endblock %}

-template有两个占位符(一个旁边):

{% extends "pages/page.html" %}
{% load mezzanine_tags fluent_contents_tags %}

{% block main %}{{ block.super }}
{% page_placeholder page.coremodulpage "main" role='m' %}
<aside>
    {% page_placeholder page.coremodulpage "sidepanel" role='s' %}
</aside>
{% endblock %}

- 在您的应用设置完成后,我们可以进行迁移:

-1。迁移您的应用:

python manage.py makemigrations yourappname

-2。将您的应用迁移到数据库:

python manage.py migrate

最后完成! - 尝试安装了Fluent-contents插件的新型管理页面。 - 在管理员选择核心页面中的页面类型下拉列表中。如果您已创建用于渲染fluent-contents选项卡的模板,其中占位符显示中包含下拉列表。现在,您可以选择所需的插件并创建页面的模块化内容。