When you enter text within Wagtail's rich text editor in the Wagtail Admin without any styling (e.g bold, ul, italic), that text (when published) appears 'coated' or embedded in <p> tags. The text is also saved in the database with the <p> tags around the text content.
I would however like to access the text without having the <p> tags around. How can I do this please? From the research that I've made, it's quite clear that wagtail has made this the default behavior [and for obvious general use case reason], however, it would have been nice if it was also possible to access text without the bounding <p> tags.
I would like to access the content within the template so that it can be viewed from the frontend. Having a <p> tag goes a long way to hurt my CSS and working against wagtail's default design is like hunting in darkness
I've tried StreamFields and StructBlocks. The content still has the <p> tag. There seems to be no way to get rid of the <p> tags and access the required information.
Thanks and I hope a favourable reply.
If you don't specifically need rich text features then using a RAWHtmlBlock instead might fit the bill. For example:
body = StreamField([
('raw', RawHTMLBlock()),
])
will output:
<div class="block-raw">Your unmodified content</div>
If you need to remove only <p> while keeping the other markup tags, you could look at registering a RewriteHandler that would strip out the <p> tags for rendering. See
Rich text internals
Another crude way of doing the same would be to define a custom block template that strips out the <p>s before rendering {{ value }}.
# models.py
body = StreamField([
('bare', RichTextBlock(template='blocks/bare.html'))
])
# templates/blocks/bare.html
<script>
/* Do something with {{ value }} */
document.write(new_value);
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With