In my django application, I am manually rendering a page and giving it to a template to include :
def get_context_data(self, **kwargs):
page = render(self.request, test_absolute_path, context, content_type=None, status=None, using=None)
soup = BeautifulSoup(page.content, 'html.parser')
soup.dosomestuff()
page.content = str(soup.decode()).replace('\n','')
context['subtests'].append(page)
return context
Then including the rendered HTML into a template using the safe
tag :
{{ page.content | safe }}
I do have my tags included but the text appears like a bytearray and the encoding isn't right for some reason :
b'
My text Cat\xc3\xa9gorisation S\xc3\xa9quqsdazeences R\xc3\xa9ponses associ\xc3\xa9es Fluidit\xc3\xa9
Notice that I also had to replace all \\n
with nothing in the code.
EDIT :
I noticed that encoding the soup in ascii at least prints all the characters, I still can't get rid of the \\n
or b
though :
page.content = soup.encode('ascii')
The page.content
always returns byte array. One option is to call decode in template tag.
{{ page.content.decode | safe }}
Another one is to use different name like below.
def get_context_data(self, **kwargs):
page = render(self.request, 'viewbase/sub_page.html', context,
content_type=None, status=None, using=None)
soup = BeautifulSoup(page.content, 'html.parser')
soup.dosomestuff()
page.new_content = soup.decode()
context['subtests'].append(page)
return context
With that, the template has below tag.
{{ page.new_content | safe }}
Or directly put content instead of page in context in case you don't need anything else from the page.
context['subtests'].append(soup)
{{ soup | safe }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.