开发者

render cms page within another page

开发者 https://www.devze.com 2023-03-14 22:18 出处:网络
Im trying to render a cms page, within another page using a custom cms plugin. The this is my plugin class:

Im trying to render a cms page, within another page using a custom cms plugin. The this is my plugin class:

class PageInDiv(CMSPlugin):
    page_div = models.ForeignKey(Page, verbose_name= "page")

    def __unicode__(self):
        return self.page_div.get_title()

as you can see all it does is link the plugin to a page then on my cms_plugins.py i have

class PageInDivPlugin(CMSPluginBase):
    model = PageInDiv
    name = _("Page in div")
    render_template = "page.html"
    admin_preview = False

    def render(self, context, instance, placeholder):
        temp = loader.get_template(instance.page_div.get_template())
        html = temp.render(context)
        context.update({
            'html': html,
            'title':instance.page_div.get_title(),
            'placeho开发者_Go百科lder':placeholder,
        })
        return context

as you can see i pass the html for the provided page to the plugin template, then the plugin template is rendered within the page thats hosting the plugin. The problem i am having is that the placeholder content from the page thats selected via foreignkey is not being rendered ( displayed ). So my question is, is there a way to render a pages placeholders programatically ?


Just for a moment ignoring the idea of creating a custom plugin in order to do what you describe (ie, render a page's placeholders programatically), the following might be a viable alternative, depending on what exactly you are trying to achieve...

You should be able, just in the template for your "outer" cms page (ie, the page within which you want to display the contents of another cms page), to get access to the current page like this:

{{ request.current_page }}

This is by virtue of the cms page middleware. So taking that a step further, you should be able to access the page's placeholders like this:

{% for placeholder in request.current_page.placeholders %}
    {{ placeholder.render }}
{% endfor %}

That's one way you could go about rendering a page's placeholders "inside" another page.


I needed to render another page from within the template which could be accomplished with:

@register.simple_tag(takes_context=True)
def render_page(context, page, default="base.html"):
    if not page:
        return loader.get_template(default).render(context)
    new_context = copy(context)
    new_context['request'] = copy(context['request'])
    new_context['request'].current_page = page
    new_context['current_page'] = page    
    new_context['has_change_permissions'] = page.has_change_permission(context['request'])
    new_context['has_view_permissions'] = page.has_view_permission(context['request'])
    if not new_context['has_view_permissions']:
         return loader.get_template(default).render(context)
    return loader.get_template(page.get_template()).render(new_context)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号