I am new to Python and Flask. I have a templates folder in the the root of my application whic has two file.
<!DOCTYPE html> <html lang="en"> <head> <title>{% block title %}{% endblock title %}</title> <link href="http://netdna.bootstrapcdn.com/bootswatch/2.3.2/united/bootstrap.min.css" rel="stylesheet"> <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/ bootstrap-responsive.min.css" rel="stylesheet"> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> </head> <body> <div id="main"> <div class="utility-nav navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> {# Navbar goes here. #} </div> </div> </div> <div class="content container"> {% block main %}{% endblock main %} </div> </div> </body> </html>
and
{% extends 'base.html' %} {% block title %}Page Title{% endblock title %} {% block main %} <h2>This is a child template.</h2> {% endblock main %}
And then i have the following function
from flask.ext.restful import Resource,request,reqparse from app.business.login import Login from app.business.appointments import Appointments from app.models.models import User, Address,Appointment from flask import render_template class AppointmentController(Resource): def __init__(self): pass def get(self): return render_template('index.html')
so when i start the server up and say http://0.0.0.0:5000/appointment
i get
"<!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <title>Page Title</title>\n \n <link href=\"http://netdna.bootstrapcdn.com/bootswatch/2.3.2/united/bootstrap.min.css\" rel=\"stylesheet\">\n <link href=\"http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/ bootstrap-responsive.min.css\" rel=\"stylesheet\">\n <script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>\n <script src=\"http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js\"></script>\n</head>\n<body>\n <div id=\"main\">\n <div class=\"utility-nav navbar navbar-fixed-top\">\n <div class=\"navbar-inner\">\n <div class=\"container\">\n \n </div>\n </div>\n </div>\n <div class=\"content container\">\n \n\t<h2>This is a child template.</h2>\n\n </div>\n </div>\n</body>\n</html>"
Meaning the templates are working but the browser is treating the response as a String and not html. What am i doing wrong.
Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.
render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.
I modified the get accordingly. Setting the content type did the trick.
from flask import render_template, make_response class AppointmentController(Resource): def __init__(self): pass def get(self): headers = {'Content-Type': 'text/html'} return make_response(render_template('index.html'),200,headers)
Solution -
def get(self): #headers = {'Content-Type': 'text/html'} #return make_response(render_template('login.html'), 200, headers) # or just return make_response(render_template('index.html')) # or def get(self): #return Response(response=render_template('index.html'), status=200, mimetype="text/html") # or just return Response(response=render_template('index.html'))
Explanation -
The "render_template" function typically returns a string output of rendered template file.
Flask creates response objects for every request. Flask's application instance has a make_response() function, which takes the return value from the route function and populates a Response object with them.
Or we can return the Response class object explicitly as shown in the second example.
Response class has some default attributes if not explicitly provided due to this reason in above examples we can skip the status and headers params. A Response class looks something like,
class Response(BaseResponse): status = 200 mimetype = "text/html"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With