Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
WYH
wyh_web_tools
提交
b596deef
W
wyh_web_tools
项目概览
WYH
/
wyh_web_tools
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wyh_web_tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b596deef
编写于
8月 15, 2021
作者:
鞋子不会飞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
视图函数可以返回str/bytes, 并且都会最终包装为Response响应对象
上级
26219699
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
96 addition
and
5 deletion
+96
-5
main.py
main.py
+87
-5
user/views.py
user/views.py
+9
-0
未找到文件。
main.py
浏览文件 @
b596deef
# 导入Flask类
import
json
import
sys
from
flask
import
Flask
from
flask
import
Flask
,
render_template
,
make_response
,
Response
,
jsonify
,
redirect
,
url_for
from
flask
import
request
from
common.converter.converter
import
MobileConverter
...
...
@@ -61,10 +61,10 @@ def index():
# print(request.form.get('username'))
# 获取post文件
#
file_obj = request.files.get('avatar') # type: FileStorage
#
file_obj.save("img01.jpg") # 将上传的文件保存到本地
#
data_bytes = file_obj.read() # 读取出上传的文件数据 bytes
#
print(data_bytes)
file_obj
=
request
.
files
.
get
(
'avatar'
)
# type: FileStorage
file_obj
.
save
(
"img01.jpg"
)
# 将上传的文件保存到本地
data_bytes
=
file_obj
.
read
()
# 读取出上传的文件数据 bytes
print
(
data_bytes
)
# 获取文本数据
# data_bytes = request.data # 返回bytes类型
...
...
@@ -122,6 +122,88 @@ def user_info03(mob_num):
return
'user_info03 mobile is {}'
.
format
(
mob_num
)
############################################################response响应#######################################################################
"""
访问静态文件 1> 将静态文件放入static目录中 2> 访问的URL xx/static/文件名
"""
"""
后端模板渲染
1> 将模板文件放入templates目录中, 并设置模板文件夹及模板语言
2> 使用render_template进行模板渲染
3> 在模板文件中替换内容
"""
############################################################response响应#######################################################################
# @app.route('/')
# def index():
# keyword = "黑马"
#
# return render_template('baidu.html', kw=keyword)
"""自定义响应对象"""
@
app
.
route
(
'/demo2'
)
def
demo2
():
# 视图函数可以返回str/bytes, 并且都会最终包装为Response响应对象
# 手动创建响应对象 主要目的为设置响应头
response
=
make_response
(
"demo_test"
)
# type: Response
response
.
headers
[
'a'
]
=
10
return
response
"""返回json"""
@
app
.
route
(
'/home/demo333333'
)
def
demo3
():
dict1
=
{
'name'
:
'zs'
,
'age'
:
20
}
# json_str = json.dumps(dict1) # 没有指定content-type
# return json_str
# 可以将字典转为json字符串, 并且自动设置content-type为application/json
# return jsonify(dict1)
return
jsonify
(
name
=
'zs'
,
age
=
20
)
# 也支持关键字实参的形式
"""重定向"""
@
app
.
route
(
'/demo4'
)
def
demo4
():
# return redirect('http://www.baidu.com')
# return redirect('/demo333333')
# 根据视图函数的标记获取URL
# url1 = url_for('demo3')
# print(url1)
# 如果想要获取视图函数的动态URL, 必须手动设置路由变量的值
url2
=
url_for
(
"demo5"
,
user_id
=
11
)
print
(
url2
)
# URL发生变化, 代码也不需要修改, 有利于URL重构
return
redirect
(
url_for
(
"demo3"
))
@
app
.
route
(
'/demo5/<user_id>'
)
def
demo5
(
user_id
):
return
"demo5"
"""自定义状态码
目的是为了和前端进行交互, 可以制定一套自定义的交互规则
"""
@
app
.
route
(
'/demo6'
)
def
demo6
():
# return 响应体, 状态码, 响应头
return
"demo6"
,
700
,
{
"b"
:
20
}
# Flask应用程序实例的run方法启动WEB服务器
if
__name__
==
'__main__'
:
print
(
app
.
url_map
)
...
...
user/views.py
浏览文件 @
b596deef
...
...
@@ -22,3 +22,12 @@ def basic_info():
def
get_articles
():
channel_id
=
request
.
args
.
get
(
'channel_id'
)
return
'you wanna get articles of channel {}'
.
format
(
channel_id
)
@
user_blu
.
route
(
'/upload'
,
methods
=
[
'POST'
])
def
upload_file
():
f
=
request
.
files
[
'pic'
]
# with open('./demo.png', 'wb') as new_file:
# new_file.write(f.read())
f
.
save
(
'./demo.png'
)
return
'ok'
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录