And*_*Dev 12 python jinja2 flask
我是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" 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>
Run Code Online (Sandbox Code Playgroud)
和
{% extends 'base.html' %}
{% block title %}Page Title{% endblock title %}
{% block main %}
<h2>This is a child template.</h2>
{% endblock main %}
Run Code Online (Sandbox Code Playgroud)
然后我有以下功能
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')
Run Code Online (Sandbox Code Playgroud)
所以,当我启动服务器并说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>"
Run Code Online (Sandbox Code Playgroud)
这意味着模板正在运行,但浏览器将响应视为字符串而不是html.我究竟做错了什么.
And*_*Dev 26
我相应地修改了get.设置内容类型就可以了.
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)
Run Code Online (Sandbox Code Playgroud)
解决方案 -
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'))
Run Code Online (Sandbox Code Playgroud)
解释 -
“render_template”函数通常返回渲染模板文件的字符串输出。
Flask 为每个请求创建响应对象。Flask 的应用程序实例有一个 make_response() 函数,它从路由函数中获取返回值并用它们填充一个 Response 对象。
或者我们可以显式返回 Response 类对象,如第二个示例所示。
响应类有一些默认属性,如果由于这个原因在上面的例子中没有明确提供,我们可以跳过状态和标题参数。响应类看起来像,
class Response(BaseResponse):
status = 200
mimetype = "text/html"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10077 次 |
| 最近记录: |