提交 303b567a 编写于 作者: Amoor123's avatar Amoor123

'20211020cookie版本'

上级 4b6ed25e
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
<h3 style="color: red">您的请求走丢了</h3>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单参数测试</title>
</head>
<body>
<div><h3>表单参数测试</h3></div>
<form action="/user/formtest" method="post">
<div><label>用户:</label><input name="username" placeholder="用户名"></div>
<div><label>密码:</label><input name="password" placeholder="密码" type="password"></div>
<div><button type="submit">提交</button></div>
</form>
<p>用户名:{{ username }}</p>
<p>密码:{{ password }}</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户测试</title>
</head>
<body>
<p>
{{ number }}
</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看请求数据</title>
</head>
<body>
<p>
request.method:{{ request.method }}
</p>
<p>
request.scheme:{{ request.scheme }}
</p>
<p>
request.server:{{ request.server }}
</p>
<p>
request.root_path:{{ request.root_path }}
</p>
<p>
request.path:{{ request.path }}
</p>
<p>
request.query_string:{{ request.query_string }}
</p>
<p>
request.headers:{{ request.headers }}
</p>
<p>
request.remote_addr:{{ request.remote_addr }}
</p>
<p>
request.environ:{{ request.environ }}
</p>
<p>
request.shallow:{{ request.shallow }}
</p>
<p>
request.url_rule:{{ request.url_rule }}
</p>
<p>
request.view_args:{{ request.view_args }}
</p>
<p>下面的都是没有打印的</p>
<p>
request.args:{{ request.args }}
</p>
<p>
request.form:{{ request.form }}
</p><p>
request.cookies:{{ request.cookies }}
</p><p>
request.files:{{ request.files }}
</p><p>
request.url:{{ request.url }}
</p><p>
request.base_url:{{ request.base_url }}
</p><p>
request.host_url:{{ request.host_url }}
</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>响应信息</title>
</head>
<body>
<p>
response.headers.:{{ response.headers }}
</p>
<p>
response._status .:{{ response._status }}
</p>
<p>
response._status_code.:{{ response._status_code }}
</p><p>
response.direct_passthrough.:{{ response.direct_passthrough }}
</p><p>
response._on_close.:{{ response._on_close }}
</p><p>
response.response.:{{ response.response }}
</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户测试</title>
</head>
<body>
<p>
{{ username }}:{{ password }}
</p>
</body>
</html>
\ No newline at end of file
from flask import Blueprint
from flask import Blueprint, redirect
from flask import request,render_template
from models.user_model import UserModel
from models import db
......@@ -19,4 +19,134 @@ def login():
user.auth_key='123456'
db.session.add(user)
return render_template('user.html',**data)
#mainapp/templates/user.html
\ No newline at end of file
#mainapp/templates/user.html
#读取链接的参数
@blue.route('/pathtest',methods=['GET','POST'])
def pathtest():
username=request.args.get('username','tom')
password=request.args.get('password','123456')
return render_template('test.html',**locals())
@blue.route('/formtest',methods=['GET','POST'])
def formtest():
username='还未提交过'
password='还未提交过'
if request.method=='POST':
username=request.form.get('username')
password=request.form.get('password')
if username.strip()=='':
username='未填写'
if password.strip()=='':
password='未填写'
return render_template('formtest.html',**locals())
@blue.route('/converttest/<path:number>',methods=['GET','POST'])
def convert(number):
print(number)
return render_template('pathtest.html',**locals())
from flask import url_for
@blue.route('/urlfor',methods=['GET','POST'])
def urlfor():
return redirect(url_for('userBlue.formtest'))
# 查看请求对象
@blue.route('/myrequest',methods=['GET','POST'])
def myrequest():
for k in request.__dict__:
print(k)
xx=request.method
return render_template('requesttest.html',**locals())
#用make_respone方法生成响应
from flask import make_response
@blue.route('/myresponse',methods=['GET','POST'])
def myresponse():
data={
'name':'tom',
'age':'18'
}
code=200
response=make_response(data,code)
for i in response.__dict__:
print(i)
response.headers['tt']='dsdsd'
return render_template('response.html',**locals())
# 用Response类生成响应
from flask import Response
import json
@blue.route('/myresponse1',methods=['GET','POST'])
def re2():
data={
'name':'tom',
'age':'18'
}
data=json.dumps(data)
code=200
response=Response(data,code,content_type='application/json;charset=utf8')
return response
# 用jsonify生成响应
from flask import jsonify
@blue.route('/myresponse2',methods=['GET','POST'])
def re3():
data={
'name':'tom',
'age':'18'
}
return jsonify(data)
# redirect 响应
@blue.route('/myresponse3',methods=['GET','POST'])
def re4():
response=redirect(url_for('userBlue.myresponse'))
response.headers['redirect']=True
return response
# 引发异常
from flask import abort
@blue.route('/catch',methods=['GET','POST'])
def catch():
abort(404)
@blue.route('/catch2',methods=['GET','POST'])
def catch2():
abort(Response('返回的响应'))
#cookie技术
@blue.route('/mycookie',methods=['GET','POST'])
def mycookie():
data={
'name':'tom',
'age':'18'
}
code=200
response=make_response(data,code)
response.set_cookie('kk','xx',)
return response
@blue.route('/deletecookie',methods=['GET','POST'])
def deletemycookie():
data={
'name':'tom',
'age':'18'
}
code=200
response=make_response(data,code)
response.delete_cookie('kk',path='/')
return response
......@@ -2,9 +2,12 @@ from mainapp import app
from mainapp.views import user_view
# from flask_script import Manager
from models import db
from flask import render_template
@app.errorhandler(404)
def notFound(error):
return render_template('404.html')
if __name__ == '__main__':
......
......@@ -8,3 +8,6 @@ class Dev():
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_ECHO = True
SECRET_KEY='aadasdo*&('
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册