feincms/feincms3

Best way to query for different page's region at template

Closed this issue · 10 comments

Like how to we query, say about page's main region at homepage's template.

What you'd probably want in this case is make the current page a descendant of the home page, use the same key for regions (for example sidebar or something), and mark the region as inherited.

Contents.inherit_regions will do the rest for you (https://github.com/matthiask/django-content-editor/blob/master/content_editor/contents.py#L54)

Respectively, content_editor.contents.contents_for_item(page, plugins, inherit_from=<queryset containing ancestors>, that is (when using django-cte-forest) contents_for_item(page, plugins, inherit_from=page.ancestors().reverse())

feincms3's TemplatePluginRenderer also supports inherit_from, so what you'd probably want to do in this case is this: https://github.com/matthiask/feincms3/blob/master/feincms3/renderer.py#L32


If you want to go low level, something like this would also work:

renderer = ... # your TemplatePluginRenderer instance
home = ... # some way to get the home page instance

contents = contents_for_item(home, renderer.plugins())

# Example:
main_region_contents = contents.main  # or contents['main'] -- does the same thing

Im trying to get items with a simple tag.

from content_editor.contents import Contents
@register.simple_tag
def show_headline(split):
    headlines = Page.objects.filter(headline=True)[:split]
    for i in headlines:
        content = Contents(i.regions)
        for item in i.pages_richtext_set.all():
            content.add(item)
        for item in i.pages_image_set.all():
            content.add(item)
    return headlines

And in template im trying to query like this

        {% show_headline 5 as headline %}
        {% for item in headline %}
        <div class="slide">
          <div class="row">
            <div class="col-md-6">
              <div class="headline">
                <h1>{{ item.title }}</h1>
                {{ content.description }}
                <a href="{{ item.get_absolute_url }}" class=" btn btn-primary">Go to Product</a>
              </div>
            </div>
            <div class="col-md-6">
              <div class="product-photo">
                {{ content.image }}
              </div>
            </div>
          </div>
        </div>
        {% endfor %}

and no luck so far. I think im making stupid mistake again 👎
This is most important feature for me. Because i often need to query other page's regions.
I really appreciate u answer this kind of questions.

Well, you're not returning the contents objects at all!

I'd do something like this (untested):

from collections import OrderedDict

@register.simple_tag
def show_headline(split):
    pages = Page.objects.filter(headline=True)[:split]
    contents = {
        page.id: Contents(page.regions) for page in pages
    }

    for item in RichText.objects.filter(parent__in=pages):
        contents[item.parent_id].add(item)
    for item in Image.objects.filter(parent__in=pages):
        contents[item.parent_id].add(item)

    return [
        (page, contents[page.id]) for page in pages
    ]

... and in the template, use {% for page, contents in headline %}

Respectively, much shorter:

from content_editor.contents import contents_for_items

@register.simple_tag
def show_headline(split):
    pages = Page.objects.filter(headline=True)[:split]
    contents = contents_for_items(pages, plugins=[RichText, Image])
    return [(page, contents[page]) for page in pages]

(See http://django-content-editor.readthedocs.io/en/latest/#contents-for-items)

Thanks @matthiask . Its much simple. Btw if u don't want me to ask this kind of questions. I won't.
But i think this feature is fundamental for every business company website or such.
And be sure i will write docs for everything that i've asked for.

You're welcome, and don't worry: It's good to hear that the software is being used. Also, developer questions are often documentation bugs, so please don't hesitate to ask more!

I'm not completely sure what to write here now, especially since the TemplatePluginRenderer and its Regions class made everything so much more flexible. Maybe https://feincms3.readthedocs.io/en/latest/rendering.html is sufficient now? Or maybe it would make sense to explain all the layers that are involved in rendering feincms3 sites these days?

We should just refer to django-content-editor's docs in rendering.html for more examples. I think that's enough. If you agree we can close this issue.

Soo just read the docs again. And no need for what i asked. With this doc
https://feincms3.readthedocs.io/en/latest/renderer.html we can close this issue.

Great! Thanks. Documentation still needs work, work in progress towards an 1.0 release is here: https://feincms3.readthedocs.io/en/v1-docs/