登录之后更新导航

Posted 002邓诺斯

tags:

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

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 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)


# db.create_all()
@app.route(‘/‘)
def daohang():
    return render_template(‘daohang.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‘
        # if user:
        #     return ‘username existed‘
        # else:
        #     user=User(username=username,password=password)
        #     db.session.add(user)
        #     db.session.commit()
        return redirect(url_for(‘daohang‘))

@app.context_processor
def mycontext():
    usern=session.get(‘user‘)
    if usern:
        return {‘username‘:usern}
    else:
        return {}

@app.route(‘/logout/‘)
def logout():
    session.clear()
    return redirect(url_for(‘regist‘))

@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‘)
        # nickname=request.form.get(‘nickname‘)
        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(debug=True)

  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}首页</title>
    <link type="text/css" rel="stylesheet" href="{{ url_for(‘static‘,filename=‘/css/index.css‘) }}">
    <script src="{{ url_for(‘static‘,filename=‘js/moonlight.js‘) }}"></script>
    {% block head %}{% endblock %}
</head>
<body id="myBody">
<nav>
    <img src="http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cda6a0688.png" width="40px">
    <a href="{{ url_for(‘daohang‘) }}">首页</a>
    <a href="http://www.jianshu.com/">下载app</a>
    <input type="text" name="search">
    <button type="submit">搜索</button>
{% if username %}
    <a href="#">{{ username }}</a>
    <a href="{{ url_for(‘logout‘) }}">注销</a>
{% else %}
    <a href="{{ url_for(‘login‘) }}">登录</a>
    <a href="{{ url_for(‘regist‘) }}">注册</a>
{% endif %}
    <a href="{{ url_for(‘biaoqian‘) }}">发布问答</a>
    <img id="myOnOff" src="http://www.runoob.com/images/pic_bulbon.gif" onclick="mySwitch()" width="20px">
</nav>
{% block main %}{% endblock %}
<p>{{ username }}contextx</p>
</body>
</html>

  

以上是关于登录之后更新导航的主要内容,如果未能解决你的问题,请参考以下文章

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航

登录之后更新导航