向页面管理添加一个字段以包含菜单项的图标

时间:2013-03-20 21:57:24

标签: django django-cms

我已经允许在我的html模板中使用带有小方形png或带有字体的图标:

<a href="#"><i class="icon-search"></i>Search</a> // using font-awesome
// or
<a href="#"><img src="images/icons/search.png">Search</a> // using images

在模板top-menu.html中,我需要能够使用以下内容:

{{ child.menu_icon_font_awesome }} # in place of "icon-search"
{{ child.menu_icon_image }} # in place of "images/icons/search.png"

如何将这些变量放入菜单中的子菜单节点?

另外,如何让字段集在admin.py中工作(更不重要)?

我的menu_icons应用看起来像这样:

# models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _
from cms.models.pagemodel import Page
from django.core.files.storage import FileSystemStorage

class OverwriteStorage(FileSystemStorage):
  """
    Deletes file of same name if exists.
  """
  def _save(self, name, content):
    if self.exists(name):
      self.delete(name)
    return super(OverwriteStorage, self)._save(name, content)

  def get_available_name(self, name):
    return name

class MenuIconFontAwesome(models.Model):
  """
    Defines Font Awesome Menu Icon
  """
  page = models.ForeignKey(Page,
                           unique=True,
                           verbose_name=_("Page"),
                           editable=False)
  menu_icon_font_awesome = models.CharField(max_length=48,
                                            verbose_name="Font Awesome Menu Icon",
                                            blank=True)

class MenuIconImage(models.Model):
  """
    Defines Image Menu Icon
  """
  page = models.ForeignKey(Page,
                           unique=True,
                           verbose_name=_("Page"),
                           editable=False)
  menu_icon_image = models.ImageField('Menu Icon Image',
                                      upload_to = 'menu_icons/',
                                      blank=True,null=True)


# admin.py
from models import MenuIconFontAwesome, MenuIconImage
from cms.admin.pageadmin import PageAdmin
from cms.models.pagemodel import Page
from django.contrib import admin

class MenuIconFontAwesomeAdmin(admin.TabularInline):
  """
    Adds field for Font Awesome Menu Icon
  """
  model = MenuIconFontAwesome
  fieldsets = (
    ('Menu Icon with Font Awesome', {
      'fields': ('menu_icon_font_awesome',),
    }),
  )

class MenuIconImageAdmin(admin.TabularInline):
  """
    Adds field for Image Menu Icon
  """
  model = MenuIconImage
  fieldsets = (
    ('Menu Icon with Uploaded Image', {
      'fields': ('menu_icon_image',),
    }),
  )

PageAdmin.inlines.append(MenuIconFontAwesomeAdmin)
PageAdmin.inlines.append(MenuIconImageAdmin)

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


# views.py
# this is the part I cannot figure out

以下是使用导航修改器提示添加的内容。它给出了错误,“类型对象'MenuIconFontAwesome'没有属性'menu_icon_font_awesome'”我确信有一些显而易见的东西我不知道。

from menus.base import Modifier
from menus.menu_pool import menu_pool
from models import MenuIconFontAwesome, MenuIconImage

class MenuIconsMod(Modifier):
    """
    Add Menu Icons to the menu nodes
    """

    def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
        if post_cut:
            return nodes
        for node in nodes:
            node.menu_icon_font_awesome = MenuIconFontAwesome.menu_icon_font_awesome
            node.menu_icon_image = MenuIconImage.menu_icon_image
        return nodes

menu_pool.register_modifier(MenuIconsMod)

关于场地集;我所看到的是这样的,http://imgur.com/ubSBeB3。我无法对那些字段集获取这些名称以及如何覆盖它们进行排序。

2 个答案:

答案 0 :(得分:1)

查看菜单修饰符(https://django-cms.readthedocs.org/en/2.3.5/extending_cms/app_integration.html#navigation-modifiers),使用NavigationNode.attr属性,您应该能够添加任何自定义数据。

答案 1 :(得分:0)

我不被允许评论,所以我会尝试将其包装在这里。

你能在模板中发布一些“你允许的”代码吗?我不确定我明白你的意思。

如果您想通过django admin上传图片,则需要在models.py文件中的类中添加menu_image = models.ImageField(upload_to = '/images/'),然后通过说<img src="{{ object.menu_image.url }}" alt="menu_image_description" />将其包含在模板中。 而不是在admin.py写

from django.contrib import admin
from myproject.models import *

class MYModelNameAdmin(admin.ModelAdmin):

    list_display = ('name',)


admin.site.register(MuModelName, MyModelNameAdmin)

字体是什么?你可以编辑你的问题并包括你到现在为止的模板,admin.py和models.py吗?