Home > Back-end >  Flask Request to get HTML data from a template?
Flask Request to get HTML data from a template?

Time:05-05

Is it possible to get the html data from a template using flask requests?

For example, this is what I'm trying.

html = request.get_data(render_template('custom_templates/basic.html'))

Then pass that html info into the template:

 return render_template('index.html', html=html)

I can't view the basic.html file using the below URL (404 not found) which is why I'm trying to "render_template". http://127.0.0.1:5000/templates/custom_templates/basic.html

CodePudding user response:

I managed to do this using make_response.

from flask import make_response

resp = make_response(render_template('custom_templates/basic.html'))
html = resp.get_data(as_text=True)

If you don't add as_text=True then it returns it in Bytes.

  • Related