【发布时间】:2016-02-14 16:57:16
【问题描述】:
我有一个 django CMS 页面,我希望将渲染的 html 制作成一个 json 对象(将 CMS html 转储为 JSON),这可能吗?我希望它看起来像这样:
我该怎么做?
所以经过一番挖掘,这就是我所管理的。
CMS 有一个views.py,它有一个函数“details(request, slug)” 我使用这种方法来渲染所需的页面,通过传递我需要渲染的页面的 slug ,获取响应并将其放入 JSON 对象并返回。
from cms.views import details
def index(request, topic):
if topic == 'home':
template = details(request, '')
else:
template = details(request, topic)
content = ''
if hasattr(template, 'render'):
# TemplateResponse must call render() before we can get the content
content = template.render().content
else:
# HttpResponse does not have a render() method
content = template.content
# Send JSON response
return JsonResponse({
'createdAt': datetime.datetime.now(),
'content': content
})
还有更好的方法吗?
【问题讨论】:
-
您想将您的 HTML 代码转储为 JSON 吗?你如何想象这样的输出?
-
link 这样的事情
标签: python json django django-cms