Stripping HTML tags and replacing for newlines

19 views
Skip to first unread message

Andréas Kühne

unread,
Apr 17, 2018, 4:52:20 PM4/17/18
to django...@googlegroups.com
Hi all,

I am hoping that someone has stumbled across a solution to a problem that I have currently.

I have created an email templating system. Each template can contain placeholders that will be replaced with live data (a bit like the django templating system). When I store the templates they are stored in HTML.

Now when I send the emails I of course want to send both text content and HTML. My idea from the beginning was to take my HTML content and strip the HTML tags and then send that. I did this via the django utils that strip tags. Now this works, HOWEVER I don't get any newlines for my <br> and <p></p> tags. 

I was wondering if anyone has had any similar issues and has found a good solution? What I want to do is strip all of the html tags and at the same time replace the <br> and <p></p> tags. 

Regards, 

Andréas

Mike Dewhirst

unread,
Apr 18, 2018, 12:16:53 AM4/18/18
to django...@googlegroups.com
I made a special template tag where I encountered a similar problem but
it wouldn't suit your use case. I had to deal with newlines inserted by
users and which needed to be removed.

Maybe you could decorate the Django method and replace the newline tags
with something unique before Django strips the tags. Then replace your
unique replacements with \n after Django returns the text.

Just a thought.


>
> Regards,
>
> Andréas
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAK4qSCdiDvmZEgb-wUuZwhh5t011ij%3Dbi4co6MaERmSLkpgaaw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAK4qSCdiDvmZEgb-wUuZwhh5t011ij%3Dbi4co6MaERmSLkpgaaw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Apr 18, 2018, 12:28:02 AM4/18/18
to django...@googlegroups.com

Now when I send the emails I of course want to send both text content and HTML. My idea from the beginning was to take my HTML content and strip the HTML tags and then send that. I did this via the django utils that strip tags. Now this works, HOWEVER I don't get any newlines for my <br> and <p></p> tags. 

I was wondering if anyone has had any similar issues and has found a good solution? What I want to do is strip all of the html tags and at the same time replace the <br> and <p></p> tags. 

Why not regex substitute all of the tags you listed with newlines before stripping the HTML out? I say use regex because you can do things like specify multiple tags and account for things like case sensitivity.

I would also look at a Python HTML to plain text conversion library, something like this:


Never needed something like this, but looks interesting.

-James

Andréas Kühne

unread,
Apr 18, 2018, 3:28:15 AM4/18/18
to django...@googlegroups.com
Thanks guys - I knew I could rely on getting good suggestions even in my sleep :-)

@James: I was thinking about going with regexp, but my regexp fu isn't to great - would take me too much time to sort through the regexp required. However I am thinking about doing something along those lines anyway. I will be looking into the html2text library you pointed out as well.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages