2017-08-17 78 views
0

在模板中,我需要根据保存在模型实例中的HTML内容呈现{{variable}}。Django 1.11如何显示CMS模型中保存的HTML中的模板变量

以下是代码的精简部分。

page.html中

{% load static %} 
<html> 
    <head> 
    <styles, links, etc.> 
    <title>{{ object.title }}</title> 
    </head> 
    <body> 
    <div>{{ object.html_content }}</div> 
    </body> 
</html> 

模型

class Page(models.Model): 

    title = models.CharField(max_length=30) 
    html_content = models.TextField() 

GlobalMixin

# Used site wide for Global information. 

class GlobalMixin(object): 

    def get_context_data(self, *args, **kwargs): 
    context = super(GlobalMixin, self).get_context_data(*args, **kwargs) 
    context['variable'] = "A Global piece of information" 
    return context 

查看

from .mixins import GlobalMixin 
from .models import Page 

PageView(GlobalMixin, generic.DetailView): 

    model = Page 
    template_name = "my_app/page.html" 

    def get_context_data(self, *args, **kwargs): 
    context = super(PageView, self).get_context_data(*args, **kwargs) 
    return context 

联系& HTML内容领域
我然后输入管理员,添加新的页面,进入我的HTML内容转换成html_content场“HTML内容”按下面的例子。

<p>This is {{ variable }} that I need to display within my rendered page!</p> 

然后SAVE

BROWSER成绩

This is {{ variable }} that I need to display within my loaded page! 

我知道有Django的扁平页,但它看起来并不像它的工作对我来说,我需要在我的模板全局变量是平的网页不提供。

该模板直接使用模型内容进行渲染而无需查看。 我想我需要处理视图中的html_content字段,然后将所需的上下文变量添加到返回的上下文中或保存临时模板文件,将html_content追加到文件中,然后将其呈现。

我该如何做这项工作?
是否有一个Django打包的接口,可以用来处理我的视图中的模板?

回答

0

我解决了它。如果有其他人遇到此问题或者有更好的方法,请填写代码,请分享。

将视图类型更改为TemplateView,使用url中的slug获取模型实例,使用django.template.engines将字符串转换为模板对象,然后呈现模板对象并在上下文中返回它。

page.html中

{% load static %} 
<html> 
    <head> 
    <styles, links, etc.> 
    <title>{{ object.title }}</title> 
    </head> 
    <body> 
    <!-- <div>{{ object.html_content }}</div> --> 
    <div>{{ html_content }}</div> 
    </body> 
</html> 

views.py

from django.views.generic.base import TemplateView 
from .mixins import GlobalMixin 
from .models import Page 

# NEW 
from django.shortcuts import get_object_or_404 
from django.template import engines 

PageView(GlobalMixin, TemplateView): # generic.DetailView 

    # model = Page 
    template_name = "my_app/page.html" 

    def get_context_data(self, *args, **kwargs): 
    context = super(PageView, self).get_context_data(*args, **kwargs) 

    # NEW 
    context['object'] = get_object_or_404(Page, slug=self.kwargs['slug']) 
    t = engines['django'].from_string(context['object'].html_content) 
    context['html_content'] = t.render(context=context, request=None) 
    return context 
0

我想你可以使用自定义过滤器像下面。

文件路径

  • yourapp/templatetags/__init__.py
  • yourapp/templatetags/filters.py

filters.py

from django import template 
import re 

register = template.Library() 

@register.filter(name="change_global_variable") 
def change_global_variable(value): 
    return re.sub(r'\{\{(.*)\}\}', 'A global piece', value) 

模板

{% load static %} 
{% load filters %} //filters.py// 
<html> 
    <head> 
    <styles, links, etc.> 
    <title>{{ object.title }}</title> 
    </head> 
    <body> 
    <div>{{ object.html_content | change_global_variable}}</div> 
    </body> 
</html> 
+0

是的,这将工作,但将需要在我的应用程序中加载两次全局变量,并增加处理时间。它将在视图Mixin中为模板中所需的所有其他字段加载Globals一次,然后再通过过滤器处理模板。尽管我喜欢你的想法。 –

+0

然后,我宁愿用JavaScript处理它。 – Jayground