11.21

Posted 037吴宜珊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11.21相关的知识,希望对你有一定的参考价值。

 

登录功能完成:

  1. js:设置return
  2. html:设置
    1. form
    2. input
  3. py:
    1. @app.route设置methods
    2. GET
    3. POST
      1. 读取表单数据
      2. 查询数据库
        1. 用户名密码对:
          1. 记住用户名
          2. 跳转到首页
        2. 用户名密码不对:
          1. 提示相应错误。

session:

  1. 从`flask`中导入`session`
  2. 设置`SECRET_KEY`
  3. 操作字典一样操作`session`:增加用户名`session[‘username‘]=`username

py:

from flask import Flask,render_template,request,redirect,url_for,session
from flask_sqlalchemy import SQLAlchemy
import config

app = Flask(__name__)
app.config.from_object(config)
db=SQLAlchemy(app)

# class SQLALchemy(object):
#     pass



class User(db.Model):
    _tablename_ = user
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(20),nullable=False)
    password = db.Column(db.String(20), nullable=False)
    nickname = db.Column(db.String(20))


@app.route(/)
def index():
    return render_template(index.html)


@app.route(/login/, methods=[GET, POST])
def login():
    if request.method == GET:
        return render_template(login.html)
    else:
        username = request.form.get(username)
        password = request.form.get(password)
        # nickname=request.form.get(‘nickname‘)
        user = User.query.filter(User.username == username).first()
        if user:
            session[user]=username
            session.permanent=True
            return redirect(url_for(daohang))
        else:
            return error username or password

        return redirect(url_for(daohang))

    @app.route(/regist/, methods=[GET, POST])
    def regist():
        if request.method == GET:
            return render_template(regist.html)
        else:
            username = request.form.get(username)
            password = request.form.get(password)

            user = User.query.filter(User.username == username).first()
            if user:
                return username existed
            else:
                user = User(username=username, password=password)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for(login))

            @app.route(/biaoqian/)
            def biaoqian():
                return render_template(biaoqian.html)


if __name__ == __main__:
    app.run()

config:

import os
SECRET_KEY = os.urandom(24)
DIALECT=mysql
DRIVER=mysqldb
USERNAME=root
PASSWORD=ROOT
HOST=127.0.0.1
PORT=3306
DATABASE=mis_db

SQLALCHEMY_DATABASE_URI = mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8
SQLALCHEMY_TRACK_MODIFICATIONS = False

login.js

function config() {
    var oUname = document.getElementById("uname");
    var oUpass = document.getElementById("upass");
    var oUpass1 = document.getElementById("upass1");
    var oError = document.getElementById("error_box");
    oError.innerHTML = "<br>";
    if (oUname.value.length < 6 || oUname.value.length > 20) {
        oError.innerHTML = "用户名要6-20之位";
        return;
    } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) {
        oError.innerHTML = "首字母不能为数字";
        return;
    } else for (var i = 0; i < oUname.value.length; i++) {
        if ((oUname.value.charCodeAt(i) < 48) || (oUname.value.charCodeAt(i) > 57) && ((oUname.value.charCodeAt(i) < 97)) || oUname.value.charCodeAt(i) > 122) {
            oError.innerHTML = "只能填写数字或字母";
            return;
        }
    }
// oUpass
    if (oUpass.value.length < 6 || oUpass.value.length > 20) {
        oError.innerHTML = "密码要6-20位";
        return;
    }
    else if (oUpass.value !== oUpass1.value) {
        oError.innerHTML = "两次密码不一致";
        return false;
    }
    return true;
    window.alert("注册成功")

}

login.html

{% extends base.html %}
{% block title %}注册{% endblock %}
{% block head %}
    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/zhuce.css‘) }}">
    <script src="{{ url_for(‘static‘,filename=‘js/zhuce.js‘) }}"></script>
{% endblock %}

{% block main %}
    <div class="box">
        <h2>注册</h2>
        <form action="{{ url_for(‘zhuce‘) }}" method="post">
            <div class="input_box">
                <input id="uname" type="text" placeholder="请输入用户名" name="username">
            </div>
            <div class="input_box">
                <input id="upass" type="password" placeholder="请输入密码" name="password">
            </div>
            <div class="input_box">
                <input id="upass1" type="password" placeholder="请再次输入密码">
            </div>
            <div id="error_box"><br></div>
            <div class="input_box">
                <button onclick="fnzhuce()">注册</button>
            </div>
        </form>
    </div>
{% endblock %}

 

以上是关于11.21的主要内容,如果未能解决你的问题,请参考以下文章

11.21团队总结

11.21 个人总结

图 学习笔记(11.21)

图 学习笔记(11.21)

11.21

11.21