Ritu Singh
Problem:
I'm trying to implement a simple "plain text to html" converter, but can't figure out why it doesn't work as intended. So here's what I want to do - let's say we have a text: text = 'Hey! /n/nIt was really nice to meet you! /n/nOur team hopes to see you soon! /n/n/nBest regards, /nYour new team.' So for such text, what I want to do is:
Wrap all the text inside the <p></p> tags
Each line should be wrapped into <p></p> tags
If there's only a single /n that means that this is a new line and we just have to wrap it into p tags
but if there are multiple /n then each /n starting from 2nd should be replaced as <br> tag, and then for the last /n just wrap a line into p (see point 2)
here's the code i've made for now:
but instead it returns only p tags and no br tags, here's the output: <p><p>Hey! </p><p></p><p>It was really nice to meet you! </p><p></p><p>Our team hopes to see you soon! </p><p></p><p></p><p>Best regards, </p><p>Mojob team.</p></p>
UPD: alright guys, thanks to all of you, but i decided to move forward with empty <p></p> tags instead of <br> ones, so found a solution to work this way:
Solution:
You're trying to check if the line starts with a /n, this will never be true since str.split(x) splits the str at all occurrences of x and removes x from the str. Also you don't need to check for /ns in the for loop since every line is indeed a new line.
And you're getting empty <p></p> because you don't check if the line is empty or not
Here is the improved code:
Suggested blogs:
>How can I browser to render links in streamed content in Django?
>Access the "model" attribute for a ListView in Django for template
>How to use a function to print S3 data to HTML using Django?
>How to make a code executed after an entry is inserted, removed or updated?
>How to insert new data into the table in Django?
>Disabling default ordering in the Django admin panel
>How to migrate `DateTimeField` to `DateField` in Django?