python后端开发如何在前端显示后端的信息

为flask提供前端工程师快速查看后端传入模板参数代码 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 8时,
在前端开发人员和后端开发人员分离时,前端开发人员如何知道后端开发人员在模板中放入了什么参数一直是一个很头疼的问题,现在我们用flask自带的debug调试界面来完成这样的功能
代码片段(5)
1.&[代码]debugger.py主要用于覆盖已有的werkzeug.DebuggedApplication&&&&
# -*- coding: utf-8 -*-
__author__ = 'myth'
Created by myth on 16-3-14.
# werkzeug的版本不能太高,我本地的是werkzeug==0.10.4
__werkzeug__ = __import__('werkzeug')
class Debugger(__werkzeug__.DebuggedApplication):
def __init__(self, app, evalex=False, request_key='werkzeug.request',
console_path='/console', console_init_func=None,
show_hidden_frames=False, lodgeit_url=None):
super(Debugger, self).__init__(app, evalex=evalex, request_key=request_key,
console_path=console_path, console_init_func=console_init_func,
show_hidden_frames=show_hidden_frames, lodgeit_url=lodgeit_url)
def debug_application(self, environ, start_response):
"""Run the application and conserve the traceback frames."""
app_iter = None
app_iter = self.app(environ, start_response)
for item in app_iter:
yield item
if hasattr(app_iter, 'close'):
app_iter.close()
except Exception:
if hasattr(app_iter, 'close'):
app_iter.close()
skip = self._get_skip(environ)
# skip是错误提示从第几行追踪代码开始
traceback = __werkzeug__.debug.get_current_traceback(skip=skip, show_hidden_frames=
self.show_hidden_frames,
ignore_system_exceptions=True)
for frame in traceback.frames:
self.frames[frame.id] = frame
self.tracebacks[traceback.id] = traceback
start_response('500 INTERNAL SERVER ERROR', [
('Content-Type', 'text/ charset=utf-8'),
# Disable Chrome's XSS protection, the debug
# output can cause false-positives.
('X-XSS-Protection', '0'),
except Exception:
# if we end up here there has been output but an error
# occurred.
in that situation we can do nothing fancy any
# more, better log something into the error log and fall
# back gracefully.
environ['wsgi.errors'].write(
'Debugging middleware caught exception in streamed '
'response at a point where response headers were already '
'sent.\n')
yield traceback.render_full(evalex=self.evalex,
secret=self.secret) \
.encode('utf-8', 'replace')
traceback.log(environ['wsgi.errors'])
def _get_skip(self, environ):
_skip = 13
request = environ["werkzeug.request"]
debug = request.args.get("debug", type=int, default=0)
return _skip
return skip
def debugger(debug=True):
__werkzeug__.debug.DebuggedApplication = Debugger
2.&[代码]重新加载DebuggedApplication&&&&
# 此段代码一定要在flask之前引入
from debugger import debugger
debugger(DEBUG_ENABLED)
3.&[代码]template.py主要用于重新实现flask.render_template方法&&&&
# -*- coding: utf-8 -*-
from flask import render_template, current_app, request
__author__ = 'myth'
def render(template_name_or_list, **context):
模板渲染方法
# 在调试模式下,模板参数可以通过添加debug参数来查看后端传入模板的参数变量
if current_app.debug:
debug = request.args.get("debug", type=int, default=0)
_debugger = request.args.get("__debugger__")
if debug and _debugger != "yes":
_debug = _Debugger()
_debug.template(**context)
return render_template(template_name_or_list, **context)
class _Debugger(object):
def template(self, **kwargs):
for k, v in kwargs.iteritems():
locals()[k] = v
if kwargs:
del kwargs
raise Exception("debug! keys -&[%s]" % ",".join(locals().keys()))
4.&[代码]现在后端在调用模板渲染时就直接render方法吧&&&&
return render("xxxx.html", **data)
5.&[代码]调用说明&&&&
# 现在前端同学怎么调用呢?
# 比如之前有一个路由:/test/index
# 那么现在就这么请求:/test/index?debug=1, 就ok了
# 注意:该请求只能是debug=True下生效
开源中国-程序员在线工具:
相关的代码(21)
1回/6622阅
1回/1474阅
0回/2957阅
开源从代码分享开始
黑夜无疆的其它代码python后端 和javascript 前端的数据交互处理是怎样实现的_百度知道
python后端 和javascript 前端的数据交互处理是怎样实现的
我有更好的答案
推荐Python的web端框架tornado,flask,django等。我个人经常使用tornado,Python开发web程序,与其他的语言没有什么区别。这样的问题出现的多了就明白了,你还是在后盾人,看一下吧,我就在那里学的,你老是靠这个也没有人回答你,还是自己学会了比较好,以后怎么办,是不是?⊙▽⊙
采纳率:44%
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。扫一扫体验手机阅读
python flask 框架后端如何获取前端的表单数据 文本 单选框 多选框
<span type="1" blog_id="1767207" userid='
134篇文章,24W+人气,2粉丝
大数据时代的微服务之路
¥51.00482人订阅
<span type="1" blog_id="1767207" userid='16:48 提问
python3+websocket前端传到后台中文信息,如何进行解析才不会乱码?
握手成功后前端向服务器发送报文信息,信息内容有中文。解析过程中如何解决乱码问题?
msg = decode(self.conn.recv(1024)) #接收报文
def decode(data):
if not len(data):
return False
length = data[1] & 127
if length == 126:
mask = data[4:8]
raw = data[8:]
elif length == 127:
mask = data[10:14]
raw = data[14:]
mask = data[2:6]
raw = data[6:]
for cnt, d in enumerate(raw):
ret += chr(d ^ mask[cnt % 4])
return ret
按赞数排序
self.conn.recv(1024).decode('utf-8')
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐python 后端restful 前端怎么调取数据_百度知道
python 后端restful 前端怎么调取数据
我有更好的答案
主要就是js对数据的操作和对dom的操作。 前端的工作就是切图,展示数据到网页中。那么怎么获取数据呢?以什么格式获取数据呢?都是需要和后台交互的。 后台语言都是不一样的:php,jsp等等,我们前端js的工作就是把他们的数据拿过来显示。
采纳率:96%
来自团队:
为您推荐:
其他类似问题
您可能关注的内容
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 python后端开发 的文章

 

随机推荐