使用Flask-Restful返回呈现的模板在浏览器中显示HTML [英] Returning rendered template with Flask-Restful shows HTML in browser

查看:153
本文介绍了使用Flask-Restful返回呈现的模板在浏览器中显示HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Flask的新手。我有一个模板文件夹在我的应用程序的根有两个文件。

 <!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 =stylesheet>
< link href =http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-response.min.css =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走到这里。 #}
< / 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>这是一个子模板。< / h2>
{%endblock main%}

然后我有以下功能



pre $ from flask.ext.restful import从app.business.login导入资源,请求,重新分配
导入从应用程序登录
.business.appointments从app.models.models导入约会
导入用户,地址,约会$ b $从瓶子导入render_template
class AppointmentController(资源):
def __init __(self):
pass
def get(self):
return render_template('index.html')

所以当我启动服务器并说 http://0.0.0.0:5000/appointment 我得到

 <!DOCTYPE html> \\\
< html lang = \en \> \\\
< head> \\\
< title>页面标题< / title> \\\
\\\
< link href = \http://netdna.bootstrapcdn.com/bootswatch/2.3.2/united/bootstrap.min.css\相对= \stylesheet\\ \\> \\\
< link href = \http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-response.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> \\ \\ n< body> \\\
< div id = \main \> \\\
< div class = \utility-nav navbar navbar-fixed-top \> \\\
< div class = \navbar-inner \> \\\
< div class = \container \> \\\
\\\
< / div> \\\
< div> \\\
< / div> \\\
< div class = \content container \> \\\
\\\
\t< h2>这是一个子模板< / h2> ; \\\
\\\
< / div> \\\
< / div> \\\
< / body> \\\
< / html>

这意味着模板正在工作,但浏览器将响应视为String而不是HTML。我究竟做错了什么。

解决方案

我相应地修改了get。设置内容类型的技巧。



pre $ class AppointmentController(Resource):
def __init __(self):
$ pass b $ b def get(self):
headers = {'Content-Type':'text / html'}
return make_response(render_template('index.html'),200,头文件)


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.

解决方案

I modified the get accordingly. Setting the content type did the trick.

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

这篇关于使用Flask-Restful返回呈现的模板在浏览器中显示HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆