2013-10-11 31 views
5

我是Python和Flask的新手。我有一個模板文件夾在我的應用程序的根目錄有兩個文件。用Flask-Restful返回呈現的模板在瀏覽器中顯示HTML

<!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> 

{% extends 'base.html' %} 
{% block title %}Page Title{% endblock title %} 
{% block main %} 
    <h2>This is a child template.</h2> 
{% endblock main %} 

然後我有以下功能

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') 

因此,當我啓動服務器,並說http://0.0.0.0:5000/appointment我得到

"<!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>" 

含義模板ar e工作,但瀏覽器將響應視爲字符串而不是html。我究竟做錯了什麼。

回答

11

我相應地修改了get。設置內容類型的確有竅門。

class AppointmentController(Resource): 
    def __init__(self): 
     pass 
    def get(self): 
     headers = {'Content-Type': 'text/html'} 
     return make_response(render_template('index.html'),200,headers) 
+0

擴展名是「.html」嗎? – tbicr

相關問題