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) @app.route('/') def home(): return render_template('shouye.html') @app.route('/sign_in/',methods=['GET','POST']) def sign_in(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: if user.password == password: session['user']=username session.permanent = True return redirect(url_for('home')) else: return 'password error' else: return 'username is not existed.' @app.route('/sign_up/',methods=['GET','POST']) def sign_up(): if request.method == 'GET': return render_template('register.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: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('sign_in')) @app.route('/question/') def question(): return render_template('question.html') @app.context_processor def mycontext(): username=session.get('user') if username: return {'username':username} else: return {} @app.route('/logout/') def logout(): session.clear() return redirect(url_for('shouye')) if __name__ == '__main__': app.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> {% block title %} {% endblock %} Home</title> <link rel="stylesheet" href="{{ url_for('static',filename='css/base css.css')}}"> <script src="{{ url_for('static',filename='js/base js.js') }}"></script> <base target="_blank" /> {% block head %} {% endblock %} </head> <body id="myBody"> <nav class="nav"> <ul> <div> {% if username %} <li><a href="#"style="margin-right: -10px"><img src="../static/images/b219ebc4b74543a96f1bc1d41d178a82b90114a1.jpg">登陸</a></li> <li><a href="{{ url_for('logout') }}"><img src="../static/images/b219ebc4b74543a96f1bc1d41d178a82b90114a1.jpg">註銷</a></li> {% else %} <a href="{{ url_for('sign_in') }}" style="margin-right: -10px"><img src="../static/images/b219ebc4b74543a96f1bc1d41d178a82b90114a1.jpg">登陸</a> <a href="{{ url_for('sign_up') }}" >註冊</a> {% endif %} </div> <img id="on_off" onclick="mySwitch()" src="../static/images/bulbon.jpg" width="50px"> <button style="float: right;margin: 5px auto;border-radius: 5px;height: 30px" type="submit">搜索</button> <input style="float: right;margin: 5px auto;border-radius: 8px;width: 200px;height: 20px;" type="text" name="search"PLACEHOLDER="輸入要搜索的內容"> </ul> </nav> {% block main %} {% endblock %} </body> </html>
{% extends'base.html' %} {% block title %} Question {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/question css.css')}}"> {% endblock %} {% block main %} <div class="question"> <p class="name">{{ username }}</p> <h3>發佈問答</h3> <form class="biao"> <div class="form-group"> <label for="ask">Question</label> <br> <textarea class="form-control" rows="1" id="ask"></textarea> </div> <br> <div class="form-group"> <label for="questionDetail">Detail</label> <br> <textarea class="form-control" rows="8" id="questionDetail"></textarea> </div> <p></p> <input type="checkbox">check me <br><br> <button type="submit" class="btn-default">發佈</button> </form> </div> {% endblock %}