3

I'd like to view a Django template in the browser. This particular template is called in render_to_string, but is not connected to a view, so I can't just runserver and navigate to the URL at my localhost.

My idea was to simply call render_to_string in the Django shell and somehow pass the resulting string to a web browser such as Chrome to view it. However, as far as I can tell the webbrowser module only accepts url arguments and can't be used to render strings representing HTML.

Any idea how I could achieve this?

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526

4 Answers4

4

Use Data URL:

import base64

html = b"..."

url = "text/html;base64," + base64.b64encode(html)
webbrowser.open(url)
Marat
  • 15,215
  • 2
  • 39
  • 48
  • 2
    I'm having a bit of trouble reproducing this snippet; it seems like [`base64.encode()`](https://docs.python.org/3.6/library/base64.html#base64.encode) expects two arguments, `input` and `output`. Did you mean `b64encode()`? Also the latter returns a byte string, which can't simply be 'added' to a regular string in Python 3. – Kurt Peek Apr 03 '18 at 21:54
  • Sorry, I was writing from memory and of course I've mistaken. It should be `b64encode` instead, I updated the answer – Marat Apr 03 '18 at 22:16
  • On Windows you may well get the error "ValueError: startfile: filepath too long for Windows". See my solution below... Also although explained, this answer still reads incorrectly. I think the penultimate line should be: `url = "text/html;base64," + base64.b64encode(r.text.encode()).decode()` – Peter F Sep 14 '22 at 13:32
  • @PeterF `html` is supposed to be a byte-like string. This answer comes from the time when Python2 was dominant and strings were bytes; I guess it's time to update the answer – Marat Sep 14 '22 at 17:59
0

you could convert the html string to url:

https://docs.python.org/2/howto/urllib2.html

optimus_prime
  • 1,039
  • 2
  • 10
  • 21
0

Following Launch HTML code in browser (that is generated by BeautifulSoup) straight from Python, I wrote a test case in which I write the HTML to a temporary file and use the file:// prefix to turn that into a url accepted by webbrowser.open():

import tempfile
import webbrowser
from django.test import SimpleTestCase
from django.template.loader import render_to_string


class ViewEmailTemplate(SimpleTestCase):
    def test_view_email_template(self):
        html = render_to_string('ebay/activate_to_family.html')
        fh, path = tempfile.mkstemp(suffix='.html')
        url = 'file://' + path

        with open(path, 'w') as fp:
            fp.write(html)
        webbrowser.open(url)

(Unfortunately, I found that the page does not contain images referenced by Django's static tag, but that's a separate issue).

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
0

Here's a more concise solution which gets around the possible ValueError: startfile: filepath too long for Windows error in the solution by @marat:

from pathlib import Path

Path("temp.html").write_text(html, encoding='utf-8')
webbrowser.open("temp.html")
Path("temp.html").unlink()
Peter F
  • 793
  • 8
  • 10
  • If you want to store it in the filesystem, I guess [`tempfile.NamedTemporaryFile](https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile) could simplify this a bit – Marat Sep 14 '22 at 17:56
  • Nice! Thanks for sharing that... I learned something new :) – Peter F Sep 16 '22 at 07:18