开发者

Rendering HTML in web.py

开发者 https://www.devze.com 2023-03-26 20:29 出处:网络
I am handling with a Wikipedia-like project. I can convert the text file to html code using the markdown. My problem is, I want to render this html code in a html file. Here is my code,

I am handling with a Wikipedia-like project. I can convert the text file to html code using the markdown. My problem is, I want to render this html code in a html file. Here is my code,

class articles:
    def GET(self):
        form_name=web.input()
        article_name=form_name.page
        article_file_path=os.path.join('articles',article_name)
        fp = open(article_file_path,'rU')
        text = fp.read()
        body = markdown2.markdown(text)
        return render.article_files(article_name, body)

I'm passing article_name and body(html code) to article_files.html. The body looks like,

<h1>Hai</h1>
<p>Welcome<em>Ajay</em></p>

The problem is, the body displays as it is. That is the html code is printed in the screen with all tags. I want to render this html code (body) like,

Hai

Welcome Ajay

My HTML file is:

$def with(title,content)
<html>
<head>
<title>$title</title>
</head>
<body>
    <form name="form" method="GET">
        $content
    </form&开发者_StackOverflow中文版gt;
</body>
</html>


HTML escaping is on by default in web.py templates. To turn it off, prepend the variable name with a colon:

<form name="form" method="GET">
    $:content
</form>

Make sure there is no way for a potentially malicious user to feed arbitrary HTML into your unescaped templates.


You need to specify the mime type of the date you are sending to the browser, otherwise it doesn't know how to display it.

You can do this by adding the following line to your function:

web.header('Content-Type', 'text/html')
0

精彩评论

暂无评论...
验证码 换一张
取 消