1

在我的 html 模板中,我允许使用带有小方形 png 或 font-awesome 的图标:

<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。我只是无法排序为什么这些字段集会获得这些名称以及如何覆盖它们。

4

2 回答 2

1

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

于 2013-03-30T23:44:48.263 回答
0

我不允许发表评论,所以我会尝试在这里总结一下。

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

如果你想通过 django admin 上传图片,你需要menu_image = models.ImageField(upload_to = '/images/')在你的 models.py 文件中的类中有一个,然后通过说将它包含在模板中<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 吗?

于 2013-03-21T11:09:24.267 回答