我想创建一个Django CMS插件,主要作为一个TextPlugin (djangocms-text-ckeditor)的孩子使用。它的目的是返回应用程序页面的链接。
为此,我按照文档中的描述创建了CMSPluginBase的子类。看起来CMSPluginBase依赖于每个插件都有自己的模板。
我必须有一个template.html文件吗?或者我是否可以为CMSPluginBase子类编写一个方法,直接返回呈现的html (本质上是一些非常简单的东西,比如'<a href="/some/page">App link</a>'),并避免调用要呈现的模板?
非常感谢你的帮助!
发布于 2016-06-04 05:04:23
想明白了!
看起来render_template不一定非要是字符串。它也可以是一个django.template.Template实例。所以,我们开始吧:
from django.template import Template
class MyLinkPlugin(CMSPluginBase):
render_template = Template('<a href="{{link}}">{{anchor}}</a>')
def render(self, context, instance, placeholder):
context['link']='http://google.com'
context['anchor'] = 'Google me'
return(context)https://stackoverflow.com/questions/37401684
复制相似问题