Skip to main content

Posts

Showing posts with the label html

[Django CMS] Adding plugins inside plugins programatically

I have a task to migrate a website. Old one is plain HTML and new one is Django CMS. I have a script that parses an HTML page from old website in to CMS page and places it into proper place. Task is to migrate all the page content (that is a CMS TextPlugin) into an interlinked pages setup. Like we have text <p>blah blah </p><a href="/page/url">Text</a><p> some other text</p> And I need to change it into CMS LinkPlugin that is nested inside of the TextPlugin. It have become a common standard in the Django CMS world now. Note we need to have a LinkPlugin because of the requirement to interlink pages. E.g. <a href="/page/url"> is a link to CMS Page object. The solution is divided into two major parts. First we need to have the link plugin added to a placeholder of the text plugin. It must also be nested by the TextPlugin, like Django CMS admin web UI would do.  So our plugin would look somehow like this in the CMS Pag...

Don't put html, head and body tags automatically into beautifulsoup content

Was making a parser recently with BeautifulSoup. Came to the final with rendering contents of the edited text. Like so: text = "<h1>Test text tag</h1>" soup = BeautifulSoup(text, "html5" ) text = soup . renderContents() print text It renders those contents with a result wrapped into the <html>, <head> and <body> tags. So print output looks like so: '<html><head></head><body><h1>Test text tag</h1></body></html>' That's a feature of the html5lib library, it fixes HTML that is lacking, such as adding back in missing required elements. The workaround is simple enough: text = soup . body . renderContents() This solution will return an inside of the <html> tag <body>. Result is: text = '<h1>Test text tag</h1>'

Django adding custom widget to Django Admin

Sometimes you need to get out of standard behaviour of your Django admin. It means expanding its functionality with custom things. In our case we have a model that we want to add a custom html element. Button that must load something via AJAX from another place to be exact. We have a typical Django polls application for simplicity. Here is its structure: First of all we need to create a structure. So here comes our model: # models.py from django.db import models class Poll (models.Model): question = models.CharField(max_length= 200 ) pub_date = models.DateTimeField( 'date published' ) We have to have our django application admin configuration to display that model: # admin.py from django.contrib import admin from polls.models import Poll admin.site.register(Poll) It will look in Django admin like so: You can see polls available here in admin. You can do all the typical things with Polls model. Add/Delete/Edit... Using standard D...