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

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

问题描述

我是 Python 和 Flask 的新手.我的应用程序根目录中有一个模板文件夹,其中有两个文件.

<html lang="zh-cn"><头><title>{% 区块标题 %}{% endblock 标题 %}<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><身体><div id="main"><div class="utility-nav navbar navbar-fixed-top"><div class="navbar-inner"><div class="容器">{# 导航栏到这里.#}

<div class="内容容器">{% block main %}{% endblock main %}

{% 扩展 'base.html' %}{% block title %}Page Title{% endblock title %}{% 块主要 %}<h2>这是一个子模板.</h2>{% endblock main %}

然后我有以下功能

from flask.ext.restful import Resource,request,reqparse从 app.business.login 导入登录从 app.business.appointments 导入约会从 app.models.models 导入用户、地址、约会从烧瓶导入render_template类约会控制器(资源):def __init__(self):经过定义获取(自我):返回 render_template('index.html')

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

"
 
 
 页面标题
 
 <;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">
 
 </div>
 </div>
 </div>
 <div class="content container">
 
	<h2>这是一个子模板.</h2>

 </div>
 </div>
</body>
</html>"

意味着模板正在工作,但浏览器将响应视为字符串而不是 html.我究竟做错了什么.

解决方案

我相应地修改了 get.设置内容类型可以解决问题.

from flask import render_template, make_response类约会控制器(资源):def __init__(self):经过定义获取(自我):headers = {'Content-Type': 'text/html'}返回 make_response(render_template('index.html'),200,headers)

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>
   <html lang="en">
   <head>
     <title>Page 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">
      
    </div>
   </div>
  </div>
  <div class="content container">
    
	<h2>This is a child template.</h2>

  </div>
  </div>
</body>
</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.

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)

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

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆