【问题标题】:How to set up a custom template in djangocms如何在 djangocms 中设置自定义模板
【发布时间】:2015-10-04 11:16:48
【问题描述】:

我已经在 Ubuntu 机器上成功安装了 djangocms,现在我想集成从 Envato 购买的自定义模板。

安装后,djangocms 自带了自己的简单模板文件,位于mysite/templates:

base.html

{% load cms_tags staticfiles sekizai_tags menu_tags %}
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{% block title %}This is my new project home page{% endblock title %}</title>
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.min.css">
        {% render_block "css" %}
    </head>
    <body>
        {% cms_toolbar %}
        <div class="container">
            <div class="navbar navbar-default" role="navigation">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">Project name</a>
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        {% show_menu 0 1 100 100 "menu.html" %}
                    </ul>
                </div>
            </div>
            {% block content %}{% endblock content %}
        </div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
        {% render_block "js" %}
    </body>
</html>

feature.html

{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute "page_title" %}{% endblock title %}

{% block content %}
    <div class="jumbotron">
        {% placeholder "feature" %}
    </div>
    <div>
        {% placeholder "content" %}
    </div>
{% endblock content %}

menu.html

{% load i18n menu_tags cache %}

{% for child in children %}
    <li class="{% if child.ancestor %}ancestor{% endif %}
        {% if child.selected %} active{% endif %}
        {% if child.children %} dropdown{% endif %}">
        {% if child.children %}
            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                {{ child.get_menu_title }} <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
                {% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
            </ul>
        {% else %}
            <a href="{{ child.get_absolute_url }}"><span>{{ child.get_menu_title }}</span></a>
        {% endif %}
    </li>
    {% if class and forloop.last and not forloop.parentloop %}{% endif %}
{% endfor %}

page.html

{% extends "base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute "page_title" %}{% endblock title %}

{% block content %}
    {% placeholder "content" %}
{% endblock content %}

我已阅读他们的文档,但没有找到与某些自定义模板集成相关的任何内容。谁能引导我朝正确的方向前进?


编辑1:

我已在 CMS_TEMPLATES 中添加:

CMS_TEMPLATES = (
    ## Customize this
    ('page.html', 'Page'),
    ('feature.html', 'Page with Feature'),
    ('index.html', 'oriel.io') # this is what I added
)

但什么也没发生。

【问题讨论】:

    标签: django django-templates django-cms


    【解决方案1】:

    将模板添加到设置文件中的CMS_TEMPLATES

    【讨论】:

    • 我已经这样做了(请参阅我的编辑),但什么也没发生。前两页从一开始就在那里。如果我切断前两个,我会收到一条错误消息:template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')] ValueError: too many values to unpack
    • 该错误意味着您在该设置中有错字或其他内容(其中一个值在元组中有两个以上的项目)。您确定您正确复制/粘贴了设置,并且设置文件中没有 CMS_TEMPLATES 的第二个定义吗?在 shell 中,您可以打印 settings.CMS_TEMPLATES 吗? PS:理想情况下 CMS_TEMPLATES 应该是一个元组列表,而不是元组的元组
    • 当我尝试运行python manage.py shell 时,它给了我同样的错误。我在settings.py中没有任何重复项
    • 它使用元组列表,但我的 css 和 js 文件无法识别。
    【解决方案2】:

    我最近遇到了同样的问题!所以让我试着解释一下

    1) 你应该在 admin.py 中扩展 PageAdmin

    在管理方面,当您要为页面选择模板(“高级设置”)时,默认情况下会调用 AdvancedSettingsForm。但我们也必须扩展以提供您的模板。

    class ExtendedPageAdmin(PageAdmin):
        def get_form_class(self, request, obj=None, **kwargs):
            if 'advanced' in request.path_info:
                return ExtendedAdvancedSettingsForm
            elif 'permission' in request.path_info:
                return PagePermissionForm
            elif 'dates' in request.path_info:
                return PublicationDatesForm
            return self.form
    

    别忘了注销和注册

    admin.site.unregister(Page)
    admin.site.register(Page, ExtendedPageAdmin)
    

    2) OK) 您已选择(“高级设置”),您的自定义模板中必须有选择。

    class ExtendedAdvancedSettingsForm(AdvancedSettingsForm):
        def __init__(self, *args, **kwargs):
            super(ExtendedAdvancedSettingsForm, self).__init__(*args, **kwargs)
    
        template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
        self.fields['template'] = forms.ChoiceField(choices=template_choices)
    

    3) 好的,我们看到了页面的自定义模板,但是如果你想保存它会有错误。这是因为 Page 模型。它看起来像这样:

    @python_2_unicode_compatible
    class Page(six.with_metaclass(PageMetaClass, MP_Node)):
        """
        A simple hierarchical page model
        """
        ...
        template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
        ...
        template = models.CharField(_("template"), max_length=100, 
                   choices=template_choices, default=TEMPLATE_DEFAULT
                   help_text=_('The template used to render the content.'))
    

    4) 因此,您应该在 init Page 对象期间更改 template_choice。让我们使用信号

    def after_page_init(sender, instance, **kwargs):
        instance._meta.get_field_by_name('template')[0]._choices = []
    
    def patch_cms_page():
        from cms.models import Page
        from django.db.models.signals import post_init
        post_init.connect(after_page_init, sender=Page)
    

    最后在 urls.py 中调用 patch_cms_page() )

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 2018-03-21
      • 2013-09-07
      • 2011-06-16
      • 1970-01-01
      • 2023-03-20
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多