大家好我在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
中执行此操作返回主页,但request.current_page.pagedescriptionextension
不返回任何内容,对此有任何想法。
提前感谢
答案 0 :(得分:1)
要在菜单中显示页面扩展名(例如,要启用菜单中显示的页面图标),首先需要将图标信息添加到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
)
在我的情况下,我使用下拉列表选择一个类,而不是图像本身,但如果您使用任何其他数据,原则是相同的。