如何在 django-cms 的页面中添加一些额外的字段?(在 Django 管理面板中) [英] How to add some extra fields to the page in django-cms? (in django admin panel)

查看:11
本文介绍了如何在 django-cms 的页面中添加一些额外的字段?(在 Django 管理面板中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 django-cms(在 django 管理面板中)的页面中添加一些额外的字段.如何以最简单的方式做到这一点?

I would like to add some extra fields to pages in django-cms (in django admin panel). How do this in the simplest way?

推荐答案

创建一个新的应用程序(称为 extended_cms 什么的)并在 models.py 中创建以下内容:

Create a new app (called extended_cms or something) and in models.py create the following:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from cms.models.pagemodel import Page

class ExtendedPage(models.Model):   
    page = models.ForeignKey(Page, unique=True, verbose_name=_("Page"), editable=False, related_name='extended_fields')
    my_extra_field = models.CharField(...)

然后创建一个admin.py:

from models import ExtendedPage
from cms.admin.pageadmin import PageAdmin
from cms.models.pagemodel import Page
from django.contrib import admin

class ExtendedPageAdmin(admin.StackedInline):
    model = ExtendedPage
    can_delete = False

PageAdmin.inlines.append(ExtendedPageAdmin)
try:
    admin.site.unregister(Page)
except:
    pass
admin.site.register(Page, PageAdmin)

这会将您的扩展模型作为内联添加到您创建的任何页面.访问扩展模型设置的最简单方法是创建上下文处理器:

which will add your extended model to as an inline to any page you create. The easiest way to access the extended model setttings, is to create a context processor:

from django.core.cache import cache
from django.contrib.sites.models import Site

from models import ExtendedPage

def extended_page_options(request):
    cls = ExtendedPage
    extended_page_options = None    
    try:
        extended_page_options = request.current_page.extended_fields.all()[0]
    except:
        pass
    return {
        'extended_page_options' : extended_page_options,
    }

现在您可以在模板中使用 {{ extended_pa​​ge_options.my_extra_field }} 访问当前页面的额外选项

and now you have access to your extra options for the current page using {{ extended_page_options.my_extra_field }} in your templates

实际上,您所做的是创建一个带有额外设置的单独模型,该模型用作每个 CMS 页面的内联.我以前从博客文章中得到这个,所以如果我能找到我会发布它.

Essentially what you are doing is creating a separate model with extra settings that is used as an inline for every CMS Page. I got this from a blog post previously so if I can find that I'll post it.

编辑

这里是博文:http://ilian.ini.org/extending-django-cms-page-model/

这篇关于如何在 django-cms 的页面中添加一些额外的字段?(在 Django 管理面板中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆