how to insert raw html to django-cms page using cms.api?

38 Views Asked by At

I am using django-cms and their cms.api to migrate some articles from a WordPress site.

So far I have managed to:

import requests as req
from cms.api import create_page, add_plugin
from bs4 import BeautifulSoup as bs
from datetime import datetime

raw = req.get("site_base/article/").text

soup = bs(raw)

title = soup.find("title").text
slug = soup.find("meta", {"property":"og:url"})['content'].split("site_base")[1].\
    replace("/","")
meta_desc = soup.find("meta", {"name": "description"})['content']



page = create_page(title, template='home.html', language='en', slug=slug, meta_description=meta_desc,
            created_by='Sid', publication_date=datetime.now().date())
placeholder = page.placeholders.get(slot='content')
add_plugin(placeholder, 'TextPlugin', 'en', body='hello world')

I am trying to find a way to put the raw html of the body(I have the articles in html format, if that matters) as content. I have looked up all possible resources I could think of and nothing is working.

1

There are 1 best solutions below

2
Rayees AC On

Django CMS plugin (specifically a TextPlugin in your case), you can use the djangocms-text-ckeditor


from cms.plugin_pool import plugin_pool
from djangocms_text_ckeditor.cms_plugins import TextPlugin

# Your existing code

page = create_page(title, template='home.html', language='en', slug=slug, meta_description=meta_desc,
            created_by='Sid', publication_date=datetime.now().date())
placeholder = page.placeholders.get(slot='content')

# Create a TextPlugin instance and set the raw HTML content as the body
text_plugin = add_plugin(placeholder, TextPlugin, 'en')
text_plugin.body = '<div>Your raw HTML content goes here</div>'

page.publish('en')  # Publish the page