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

How to implement django-fluent-contents to Mezzanine existing project

我有带有现有页面的现有夹层项目。是否可以在没有流畅页面功能的情况下实现 AdminPage 流畅内容功能?只想按原样创建夹层页面,但其中包含流畅的内容。这有可能实施吗?任何人都可以展示一些示例如何在夹层 AdminPage 中实现它。

因为没有人遇到过这个问题,我已经弄清楚并成功地在现有的 Mezzanine 项目中实施 Fluent-contents。 这非常简单,但调查需要编辑夹层 cms 的核心源。毕竟输出解决方案是简单的应用程序,可以在管理端或客户端扩展夹层页面。

(难度[​​=106=]: medium/expert)

解决方案:

(对于这个例子,我使用了名称为 "cms_coremodul" 的应用程序) PS: 是用ver.制作的。 Python 3.4 带虚拟环境。

夹层设置和安装:

-夹层版本 4.0.1

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

pip install django-fluent-contents

-您也可以选择安装功能强大的所见即所得 CKEditor。

pip install django-ckeditor

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

settings.py :

-fluent-contents 必须在您的应用之上,在夹层应用之下。

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-contents 的 settings.py 设置完成后让我们迁移所有内容:

python manage.py migrate

-如果有任何error/fault关于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 : - 这行添加到夹层项目的 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]

-append your app to INSTALLED_APPS (add your app to INSTALLED_APPS).

INSTALLED_APPS = (
    ...
    "yourappname_with_fluentcontents",
)

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

-带有一个占位符的模板:

coremodulpage.html:

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

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

-带有两个占位符的模板(一个放在一边):

{% 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.Make 迁移您的应用程序:

python manage.py makemigrations yourappname

-2.Make 将您的应用程序迁移到数据库:

python manage.py migrate

终于完成了! - 尝试使用安装了 Fluent-contents 插件的新型管理页面。 - 在 Admin select 核心页面的页面类型下拉列表中。如果您已经创建用于呈现带有占位符的流畅内容选项卡的模板,则显示其中带有下拉菜单。现在您可以 select 想要的插件并创建您的页面的模块化内容。