2013-03-20 80 views
0

我已經允許了,在我的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 

在模板頂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) 

關於fieldsets;我所看到的是這樣的,http://imgur.com/ubSBeB3。我只是無法排序爲什麼這些字段集會得到這些名稱以及如何覆蓋它們。

+0

這已經有一段時間了,在那段時間我已經取得了很多進展。我最堅持這最後一部分,並會感謝任何幫助。我已經將所有代碼更新爲我目前正在做的事情。我沒有找到我理解的文檔,它告訴我如何將屬性添加到菜單節點。感謝您的幫助。 – TheHerk 2013-03-30 01:35:11

回答

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嗎?

+0

感謝您的協助。我還不夠清楚,並已編輯進一步的細節。 – TheHerk 2013-03-21 16:28:37

+0

好的,所以,你可以有一個字段,因爲我建議你在哪裏上傳圖片,但是使它成爲'menu_image = models.ImageField(upload_to ='/ images /',blank = True,null = True)'並且在模板中{%child.get_menu_icon}}'do'{%if menu_image%} /> {%else%} {%endif%}'如果您上傳了它將被放置的圖像,如果沒有,其他代碼將在模板中啓用。 – Tamara 2013-03-21 20:25:02

+0

告訴我,這對你是否有意義,或者我還沒有明白你的意思:) – Tamara 2013-03-21 20:26:24

1

查看菜單修飾符(https://django-cms.readthedocs.org/en/2.3.5/extending_cms/app_integration.html#navigation-modifiers),使用NavigationNode.attr屬性,您應該可以添加任何自定義數據。

+0

非常感謝您的提示。我認爲那是票。我嘗試了幾種不同的方法來實現它。這似乎是最合乎邏輯的,儘管我無法獲得任何工作。我無法弄清楚如何調用模型中的變量,但我無法弄清楚如何將每個變量綁定到特定節點。請看看上面的內容,讓我知道你的想法。 – TheHerk 2013-04-01 01:58:11

+0

節點應該是什麼? CMS頁面還是其他?在節點對象中,您可以使用一些數據(在每個節點的'attr'屬性中)獲取正確的'MenuIconImage' /'MenuIconFontAwesome'。 AFAIK他們沒有真正的文件記錄,但你應該很容易地從代碼中獲得它們。 – yakky 2013-04-10 18:15:29

+0

那麼如果我將一個像「icon-search」這樣的字符串硬編碼到node.menu_icon_font_awesome =「icon-search」中,我可以在模板中使用{{child.menu_icon_font_awesome}}。我無法弄清楚的是如何從每個頁面獲取實際的變量到menu.py中。 – TheHerk 2013-04-10 19:30:19

相關問題