emmett-framework / emmett

The web framework for inventors
Other
1.06k stars 71 forks source link

About app.render_template #319

Closed josejachuf closed 3 years ago

josejachuf commented 3 years ago

Hi @gi0baro In reference to [1]. I see that it does not render on the browser but shows the code of the page in browser

@app.route('/test')
async def test():
    return app.render_template("test.html")

test.html is:


{{extend 'layout.html'}}

<h1>Hello World</h1>

[1] https://github.com/emmett-framework/emmett/issues/318

gi0baro commented 3 years ago

@josejachuf since 2.0 the default content-type is text/plain instead of text/html (ref: https://emmett.sh/docs/2.2.x/upgrading#version-20).

While App.route hides this when using standard template flow, whenever you manually generate string responses containing different content, you should manually set the content-type header value, eg:

@app.on_error(404)
async def not_found():
    response.content_type = "text/html"
    return app.render_template("404.html")
josejachuf commented 3 years ago

I had read it a while ago in the documentation, now I remember it. This works fine Thank you