我尝试将Django CMS占位符从页面呈现为变量,以将呈现的代码作为JSON返回。
所以我所做的是:
from cms.models.placeholdermodel import Placeholder
from cms.models.pagemodel import Page
def render(self, page_id, placeholder_slot, request):
page = Page.objects.get(id=page_id)
placeholder = page.placeholders.get(slot=placeholder_slot)
现在我想将占位符呈现给变量。我必须以哪种方式调用哪个函数才能得到它?
答案 0 :(得分:0)
您可以使用Placeholder.render
[1]进行渲染。请注意,上下文必须在HttpRequest
键下包含有效的'request'
对象。 width
可能是None
。有关示例,请参阅{% render_placeholder %}
实现[2]。
[1] https://github.com/divio/django-cms/blob/7888ab8421bb836c8f7a1127d9a2bf4d4bbdf23e/cms/models/placeholdermodel.py#L131 [2] https://github.com/divio/django-cms/blob/7888ab8421bb836c8f7a1127d9a2bf4d4bbdf23e/cms/templatetags/cms_tags.py#L1132
答案 1 :(得分:0)
Placeholder
可以手动调用并像这样渲染,例如:
from cms import models
from django import template
def render(request):
placeholder = models.Placeholder.objects.get_or_create(slot='some_slot')
context = template.RequestContext(request)
return placeholder.render(request, width=None)
或者对于json / javascript / etc.你不想要完整的html而只需要值:
def render_basic(request):
placeholder = models.Placeholder.objects.get_or_create(slot='some_slot')
context = template.RequestContext(request)
return placeholder.render(request, width=None, editable=False)
使用StaticPlaceholder
:
from cms import models
def render(request):
placeholder = models.StaticPlaceholder.objects.get_or_create(name='name of the placeholder')
return placeholder.code