1

I've extended the Django cms Page model into ExtendedPage model and added page_image to it. How can I now acces the page_image property in a template.

I'd like to access the page_image property for every child object in a navigation menu... creating a menu with images...

I've extended the admin and I have the field available for editing (adding the picture)

from django.db import models
from django.utils.translation import ugettext_lazy as _
from cms.models.pagemodel import Page
from django.conf import settings

class ExtendedPage(models.Model):   
    page = models.OneToOneField(Page, unique=True, verbose_name=_("Page"), editable=False, related_name='extended_fields')
    page_image = models.ImageField(upload_to=settings.MEDIA_ROOT, verbose_name=_("Page image"), blank=True)

Thank you!

BR

Mission
  • 1,287
  • 3
  • 16
  • 31

1 Answers1

3
request.current_page.extended_fields.page_image

should work if you are using < 2.4. In 2.4 they introduced a new two page system (published/draft) so you might need

request.current_page.publisher_draft.extended_fields.page_image

I usually write some middleware or a template processor to handle this instead of doing it repetitively in the template. Something like:

class PageOptions(object):
    def process_request(self, request):
        request.options = dict()
        if not request.options and request.current_page:
            extended_fields = None
            try:
                extended_fields = request.current_page.extended_fields
            except:
                try:
                    custom_settings = request.current_page.publisher_draft.extended_fields
                except:
                    pass
            if extended_fields:
                for field in extended_fields._meta.fields:
                    request.options[field.name] = getattr(extended_fields, field.name)
        return None

will allow you to simply do {{ request.options.page_image }}

Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • Hi, your solution works fine for the current page... I'm primarily asking for the menu... if I use child.publisher_draft.extended_fields.page_image it doesn't work... so i need this for all menu items. Are templatetags the only option for this? – Mission May 31 '13 at 08:51
  • @Mission good question. I have the same issue! How did you solve it? E.g. I need to add menu_icon field (done) and now I have to load it in show_menu. Thanks! – Daviddd Jul 12 '13 at 14:29
  • A little bit late now, but it is built in with Django CMS Beta 3. Thought it might still be of interest for users finding this question through Google: http://stackoverflow.com/a/21849360/870769 – sthzg Feb 18 '14 at 09:35