Flask做一个登陆和注册(mysql、session和两个钩子before_request和context_processor)

总之,先写html

ral.html

<form action="/login" method="post" name="login">
        
        <h1>登陆h1>
        <table>
            <tr>
                <td>用户名:td>
                <td><input type="text" name="username">td>
            tr>
            <tr>
                <td>密码:td>
                <td><input type="password" name="password">td>
            tr>
            <input type="submit" value="登陆">
        table>
        <hr>
    form>
    <form action="/register" method="post" name="register">
        
        <h1>注册h1>
        <table>
            <tr>
                <td>用户名:td>
                <td><input type="text" name="susername">td>
            tr>
            <tr>
                <td>密码:td>
                <td><input type="password" name="spassword">td>
            tr>
            <input type="submit" value="注册">
        table>
        <hr>
    form>

emmmm,html这一块应该没啥难的,毕竟写的非常简陋。

再来一个登陆后的html

log.html

<h1>登陆成功{{ username }}h1>

emmm,还行,这样就差不多了。

开始我们的Py

from flask import Flask
from flask import Flask, render_template, redirect, url_for, flash, request,session,g
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import and_
from flask_wtf.csrf import CSRFProtect
import os

app = Flask(__name__)

app.config['SECRET_KEY'] = "afsfa"
CSRFProtect(app)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:3306/userdata"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)


class User(db.Model):
	# 这里就是表了。
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128), unique=False)


@app.route('/login', methods=['POST'])
def login():
    username = request.form.get('username')
    password = request.form.get('password')
    user = User.query.filter(and_(User.username == username, User.password == password)).first()
    if user:
        session['username'] = username
        session['password'] = password
        return redirect(url_for('lsuss'))
    else:
        return '用户名或密码错误'


@app.route('/if_login', methods=['GET'])
def lsuss():
    print(g.username)
    if g.username:
        return render_template('log.html')
    else:
        return render_template('ral.html')


@app.route('/register', methods=['POST'])
def register():
    username = request.form.get('susername')
    password = request.form.get('spassword')
    user = User(username=username, password=password)
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('ral'))


@app.before_request
def test():
    username = session.get('username')
    password = session.get('password')
    g.username = None
    if username:
        user = User.query.filter(and_(User.username == username, User.password == password)).first()
        if user:
            g.username = username


@app.context_processor
def name():
    username = session.get('username')
    return {'username': username}


db.drop_all()
db.create_all()


if __name__ == '__main__':
    app.run(Debug=True)

代码重新编辑了一下,不想写注释了= =

你可能感兴趣的:(学习笔记,flask)