1、链接非关系型数据库redis
app/init.py
在app/init.py中配置链接redis数据库
import os
from flask import Flask
from flask_session import Session
import redis
from App.views import blue
def create_app():
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
templates_dir = os.path.join(BASE_DIR, 'templates')
static_dir = os.path.join(BASE_DIR, 'static')
app = Flask(__name__,
static_folder=static_dir,
template_folder=templates_dir)
app.register_blueprint(blueprint=blue, url_prefix='/app')
# 秘钥
app.config['SECRET_KEY'] = 'secret_key'
# 使用非关系型数据库redis存储信息
app.config['SESSION_TYPE'] = 'redis'
# 链接阿里云redis数据库
app.config['SESSION_REDIS'] = redis.Redis(host='IP地址',
port='6379', password='')
# 定义前缀
app.config['SESSION_KEY_PREFIX'] = 'flask'
# 种初始化app
Session(app)
return app
链接redis数据库前你需要安装第三方模块redis和Flask-Session
pip install redis
pip install Flask-Session
下面是链接redis数据库的一些基本配置
from flask-session import Session
import redis
# 秘钥
app.config[‘SECRET_KEY’] = ‘secret_key’
# 使用非关系型数据库redis存储信息
app.config[‘SESSION_TYPE’] = ‘redis’
# 链接阿里云redis数据库
app.config[‘SESSION_REDIS’] = redis.Redis(host=’IP地址’,
port=’6379’, password=”)
# 定义前缀
app.config[‘SESSION_KEY_PREFIX’] = ‘flask’
# 种初始化app
Session(app)
2、登录login
app/views.py
from flask import Blueprint, render_template, request, \
session,redirect, url_for, make_response
blue = Blueprint('app', __name__)
@blue.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
username = session.get('username')
return render_template('login.html', username=username)
else:
username = request.form.get('username')
session['username'] = username
return redirect(url_for('app.login'))
http://127.0.0.1:8080/app/login/页面如下:
在文本框中输入名字,点击提交,上面对应显示出名字,redis数据库中存对应的值。
最开始是None
父模板
templates/base.html
{% block name %}{% endblock %}通过模板标记我们可以把重复的部分写到一个基本模板base.html里,然后后面分别来继承该父模板就可以了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
{% endblock %}
</title>
{% block extCSS %}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
{% endblock %}
</head>
<body>
{% block header %}
{% endblock %}
{% block content %}
{% endblock %}
{% block extJS %}
{% endblock %}
</body>
</html>
templates/base_main.html
base_main.html继承该(base.html)模板,当后面有继承base_main.html模板的时候base.html一并也被继承下来
{% extends 'base.html' %}
{% block content %}
徐孝元
{% endblock %}
templates/login.html
登录页面
{% extends 'base_main.html' %}
{% block title %}
登录页面
{% endblock %}
{% block header %}
欢迎:{{ username }}
<form action="" method="post">
姓名:<input type="text" placeholder="请输入用户名" name="username">
<input type="submit" value="提交">
</form>
{% endblock %}
3、设置cookie和删除cookie
- 设置cookie
@blue.route('/getresponse/')
def get_response():
"""设置cookie"""
response = make_response('<h2>hello, world</h2>', 200)
s = 'abcdefghijklmnopqrstuvw1234567890'
ticket = ''
for i in range(20):
ticket += random.choice(s)
response.set_cookie('ticket', ticket)
return response
- 删除cookie
@blue.route('/deletecookie/')
def del_cookie():
"""删除cookie"""
response = make_response('<h1>xlsb</h1>')
response.delete_cookie('ticket')
return response
不管在创建cookie和删除cookie的时候都要通过make_response()来创建响应对象,响应对象通过set_cookie()和delete_cookie()方法来创建cookie和删除cookie