2017-08-17 39 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

相關問題