在 Django 中扩展页面
Extending the page in Django
大家好我在Django-cms中创建了我的第一个页面扩展我保存了信息购买我可以再次获得它
我的模型是这样的
class PageDescriptionExtension(PageExtension):
description_page = models.TextField('description',
default = None,
help_text = 'Please provide a small description about this page',
max_length = 250,
)
image = models.ImageField(upload_to='icons')
extension_pool.register(PageDescriptionExtension)
我的admin.py
是这个
class PageDescriptionExtensionAdmin(PageExtensionAdmin):
pass
admin.site.register(PageDescriptionExtension, PageDescriptionExtensionAdmin)
和我的工具栏
class PageDescriptionExtensionToolbar(ExtensionToolbar):
# defines the model for the current toolbar
model = PageDescriptionExtension
def populate(self):
# setup the extension toolbar with permissions and sanity checks
current_page_menu = self._setup_extension_toolbar()
# if it's all ok
if current_page_menu:
# retrieves the instance of the current extension (if any) and the toolbar item URL
page_extension, url = self.get_page_extension_admin()
if url:
# adds a toolbar item
current_page_menu.add_modal_item(_('Description page'), url=url,
disabled=not self.toolbar.edit_mode)
当我尝试下注元素时在我的模板中没有
{% if request.current_page.pagedescriptionextension %}
<img src="{% static request.current_page.pagedescriptionextension.image.url %}">
{% else %}
here
{% endif %}
此代码打印 here
我在主页 request.current_page
return 主页中执行此操作,但 request.current_page.pagedescriptionextension
不 return 任何内容,对此有任何想法.
提前致谢
要在菜单中显示页面扩展(例如,启用菜单中显示的页面图标),您首先需要将图标信息添加到navigation node。在下面的代码片段中,我添加了获取 pagemenuiconextension
的功能,其定义与您的页面扩展非常相似。菜单扩展需要放在cms_menus.py
:
from menus.base import Modifier
from menus.menu_pool import menu_pool
from raven.contrib.django.raven_compat.models import client
from cms.models import Page
class MenuModifier(Modifier):
"""
Injects page object into menus to be able to access page icons
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
# if the menu is not yet cut, don't do anything
if post_cut:
return nodes
for node in nodes:
try:
if "is_page" in node.attr and node.attr["is_page"]:
class LazyPage(object):
id = node.id
page = None
def pagemenuiconextension(self):
try:
if not self.page:
self.page = Page.objects.get(id=self.id)
return self.page.pagemenuiconextension
except AttributeError, ae:
# print ae
return False
except Exception, e:
print e
client.captureException()
node.pageobj = LazyPage()
else:
pass
except Exception, e:
client.captureException()
return nodes
menu_pool.register_modifier(MenuModifier)
然后您可以在菜单文件中访问它(相应菜单项的片段):
<div class="{{ child.pageobj.pagemenuiconextension.menu_navicon }}" style="height: 16px;">{{ child.get_menu_title }}</div>
页面扩展看起来像这样:
class PageMenuIconExtension(PageExtension):
class Meta:
verbose_name = _("menu and robots settings")
verbose_name_plural = _("menu and robots settings")
menu_navicon = models.CharField(
_("Page icon"),
choices=MENU_ICON_CHOICES,
blank=True,
max_length=50
)
在我的例子中,我使用的是 select 和 class 的下拉列表,而不是图像本身,但如果您使用任何其他数据,原理是相同的。
大家好我在Django-cms中创建了我的第一个页面扩展我保存了信息购买我可以再次获得它
我的模型是这样的
class PageDescriptionExtension(PageExtension):
description_page = models.TextField('description',
default = None,
help_text = 'Please provide a small description about this page',
max_length = 250,
)
image = models.ImageField(upload_to='icons')
extension_pool.register(PageDescriptionExtension)
我的admin.py
是这个
class PageDescriptionExtensionAdmin(PageExtensionAdmin):
pass
admin.site.register(PageDescriptionExtension, PageDescriptionExtensionAdmin)
和我的工具栏
class PageDescriptionExtensionToolbar(ExtensionToolbar):
# defines the model for the current toolbar
model = PageDescriptionExtension
def populate(self):
# setup the extension toolbar with permissions and sanity checks
current_page_menu = self._setup_extension_toolbar()
# if it's all ok
if current_page_menu:
# retrieves the instance of the current extension (if any) and the toolbar item URL
page_extension, url = self.get_page_extension_admin()
if url:
# adds a toolbar item
current_page_menu.add_modal_item(_('Description page'), url=url,
disabled=not self.toolbar.edit_mode)
当我尝试下注元素时在我的模板中没有
{% if request.current_page.pagedescriptionextension %}
<img src="{% static request.current_page.pagedescriptionextension.image.url %}">
{% else %}
here
{% endif %}
此代码打印 here
我在主页 request.current_page
return 主页中执行此操作,但 request.current_page.pagedescriptionextension
不 return 任何内容,对此有任何想法.
提前致谢
要在菜单中显示页面扩展(例如,启用菜单中显示的页面图标),您首先需要将图标信息添加到navigation node。在下面的代码片段中,我添加了获取 pagemenuiconextension
的功能,其定义与您的页面扩展非常相似。菜单扩展需要放在cms_menus.py
:
from menus.base import Modifier
from menus.menu_pool import menu_pool
from raven.contrib.django.raven_compat.models import client
from cms.models import Page
class MenuModifier(Modifier):
"""
Injects page object into menus to be able to access page icons
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
# if the menu is not yet cut, don't do anything
if post_cut:
return nodes
for node in nodes:
try:
if "is_page" in node.attr and node.attr["is_page"]:
class LazyPage(object):
id = node.id
page = None
def pagemenuiconextension(self):
try:
if not self.page:
self.page = Page.objects.get(id=self.id)
return self.page.pagemenuiconextension
except AttributeError, ae:
# print ae
return False
except Exception, e:
print e
client.captureException()
node.pageobj = LazyPage()
else:
pass
except Exception, e:
client.captureException()
return nodes
menu_pool.register_modifier(MenuModifier)
然后您可以在菜单文件中访问它(相应菜单项的片段):
<div class="{{ child.pageobj.pagemenuiconextension.menu_navicon }}" style="height: 16px;">{{ child.get_menu_title }}</div>
页面扩展看起来像这样:
class PageMenuIconExtension(PageExtension):
class Meta:
verbose_name = _("menu and robots settings")
verbose_name_plural = _("menu and robots settings")
menu_navicon = models.CharField(
_("Page icon"),
choices=MENU_ICON_CHOICES,
blank=True,
max_length=50
)
在我的例子中,我使用的是 select 和 class 的下拉列表,而不是图像本身,但如果您使用任何其他数据,原理是相同的。