5-登录注册功能

2018-11-08  本文已影响9人  Double_Chen

注册

@app.route('/register/', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    else:
        account = request.form.get('account')
        telephone = request.form.get('telephone')
        email = request.form.get('email')
        username = request.form.get('username')
        password = request.form.get('password')
        password2 = request.form.get('password2')
        intro = request.form.get('intro')

        # 默认头像
        head_img_url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1540902584502&di=44019bc649a2cc6a8b1dca9ba57c4015&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01460b57e4a6fa0000012e7ed75e83.png'

        if password != password2:
            return u'两次密码输入不一致'

        if account == None or account == '':
            return u'请输入手机号码'
        if password == None or password == '':
            return u'请输入密码'

        user = User.query.filter(User.account == telephone,
                                 User.password == password,
                                 User.telephone == telephone,
                                 User.email == email,
                                 User.username == username,
                                 User.head_img_url == head_img_url).first()

        if user:
            return u'该用户已存在'
        else:
            user = User(account=account,
                        password=password,
                        telephone=username,
                        email=email,
                        username=username,
                        intro=intro, )

            db.session.add(user)
            db.session.commit()
            return redirect(url_for('login'))

登录

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

        print(request.get('account'))

        user = User.query.filter(User.account == account,
                                 User.password == password).first()
        if user:
            if user.username == None or user.username == '':
                user.username = '用户%s' % (user.id)
                db.session.commit()
            session['user_id'] = user.id
            return redirect(url_for('index'))
        else:
            return u'账号或密码错误'

注销

有登录当然也要有注销,我们在登录成功后将user_id存放到了session当中,注销的时候也要将它移除掉

@app.route('/logout/')
def logout():
    session.pop('user_id')
    return redirect(url_for('login'))

@app.context_processor装饰器

@app.context_processor作为一个装饰器来修饰一个函数,该函数必须返回一个dict,之后这个dict内的key将作为变量在所有的模板中可见,我们需要通过这个功能,在前端页面登录成功后将"登录、注册"按钮变为"用户名、注销"

@app.context_processor
def my_context_processor():
    user_id = session.get('user_id')
    if user_id:
        user = User.query.filter(User.id == user_id).one()
        if user:
            return {'login_required_user': user}
    return {}

现在我们在HTML模板内可以通过jinja2框架的if语句来实现UI的变化:

{% if login_required_user %}
    <li><a  href="{{ url_for('edit_user_info') }}">{{ login_required_user.username }}</a></li>
    <li><a href="{{ url_for('logout') }}">注销</a></li>
{% else %}
    <li><a href="{{ url_for('login') }}">登录</a></li>
    <li><a href="{{ url_for('register') }}">注册</a></li>
{% endif %}

GitHub链接

上一篇下一篇

猜你喜欢

热点阅读