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