0

I've made an extra image field as shown by @Timmy O'Mahony on How to add some extra fields to the page in django-cms? (in django admin panel)

I'd like to call this image field from another page using its reverse_id, much like

{% page_attribute "extended_page_options.image" "my_page_reverse_id" %}

Do I need a custom template tag? How can I do it?

I am using django-cms 2.4.3 and django 1.5.5

EDIT:

models.py

from django.db import models
from django.utils.translation import ugettext_lazy as _
from cms.models.pagemodel import Page
from filer.fields.image import FilerImageField

class ExtendedPage(models.Model):   
    page = models.ForeignKey(Page, unique=True, verbose_name=_("Page"), editable=False, related_name='extended_fields')
    image = FilerImageField(blank=True, null=True)
Community
  • 1
  • 1
Mærcos
  • 188
  • 14

2 Answers2

1

In a recent project, I think I solved this with a context_processor as follows:

from django.conf import settings

def page_extension(request):

    """Puts settings.DEBUG and the page_extension, if present and puts it into
    the context"""

    context = {'debug': settings.DEBUG}

    if request.current_page:
        page = request.current_page

        try:
            context.update({'page_extension': page.page_extension})
        except:
            try:
                context.update({'page_extension': page.publisher_public.page_extension})
            except:
                pass

    return context

In this case, I've also used the opportunity to also add the value of settings.DEBUG, this is optional of course.

Then, just add this to your settings file as:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'path.to.above.code.page_extension',
)

Then, next step, is just access the value in your templates:

{% if page_extension and page_extension.my_page_property %}{{ page_extension.my_page_property }}{% endif %}
mkoistinen
  • 7,724
  • 3
  • 41
  • 56
  • My guess is that function only retrieves the extended field of the current page. however, you gave me an idea to try and pass the reverse_id to get the page _and_ check if it exists. Since I only need it for a couple of templates, I will try to make a custom template tag (it seems I can't avoid it) – Mærcos May 08 '14 at 13:57
  • Well, TBH, you should be able to access it directly via {{ request.current_page.publisher_public.page_extension }} or whatever. My template_context_processor is only for convenience. – mkoistinen May 08 '14 at 14:00
  • Yes, but the question was how to access the field of *another* page – Mærcos May 08 '14 at 14:04
  • 1
    That's alright. It actually gave me useful insight. – Mærcos May 08 '14 at 14:18
0

First thing, if you are starting out the new project, go for django cms 3.0, it has lots of new and exciting features than the previous release.

Back to your question, go to the advanced settings of the page which contains the actual image field and add the id. Then on other page do like this:

{% page_attribute "field_name" "my_page_reverse_id" %}
pynovice
  • 7,424
  • 25
  • 69
  • 109
  • Thanks for the tip. Already added the id ("index"). I tried retrieving the extra image field called "image" using {% page_attribute "image" "index" %} but it didn't work. – Mærcos May 08 '14 at 12:46
  • I've also tried using the models related_name to no avail (see edit). {% page_attribute "extended_fields.image" "index" %} didn't work either – Mærcos May 08 '14 at 12:52
  • It raises a Page.DoesNotExist:> A template tag couldn't find the page with lookup arguments `{'reverse_id': '', 'site': 1}`. The URL of the request was: http://example.com/ – Mærcos May 08 '14 at 13:42