from flask import Flask, render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config from functools import wraps from datetime import datetime 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(100), nullable=False) password= db.Column(db.String(500), nullable=False) class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer,primary_key=True,autoincrement=True) title = db.Column(db.String(100),nullable=False) detail = db.Column(db.Text,nullable=False) creatTime = db.Column(db.DateTime,default=datetime.now) authorID = db.Column(db.Integer,db.ForeignKey('User.id')) author = db.relationship('User',backref=db.backref('question')) class Comment(db.Model): _tablename_='comment' id = db.Column(db.Integer,primary_key=True,autoincrement=True) author_id = db.Column(db.Integer,db.ForeignKey('User.id')) question_id = db.Column(db.Integer,db.ForeignKey('question.id')) detail = db.Column(db.Text,nullable=False) creatTime = db.Column(db.DateTime,default=datetime.now) question = db.relationship('Question',backref=db.backref('comments',order_by=creatTime.desc)) author = db.relationship('User',backref=db.backref('comments')) #db.create_all() @app.route('/') def shouye(): context={ 'questions': Question.query.all() } return render_template('shouye.html',**context) @app.route('/detail/<question_id>') def question_detail(question_id): question = Question.query.filter(Question.id == question_id).first() return render_template('detail.html',question=question) @app.route('/zhuce/',methods=['GET','POST']) def zhuce(): if request.method == 'GET': return render_template('zhuce.html') else: username = request.form.get('username') # 獲取form中的數據 password = request.form.get('password') # 獲取form中的數據 email = request.form.get('email') # 獲取form中的數據 user = User.query.filter(User.username == username).first() if user: return'用戶已存在' else: user = User(username = username,password=password) db.session.add(user) db.session.commit() return redirect(url_for('denglu')) @app.route('/denglu/',methods=['GET','POST']) def denglu(): if request.method=='GET': return render_template('denglu.html') else: username = request.form.get('username') password = request.form.get('password') user=User.query.filter(User.username==username).first() if user: session['user']=username session.permanent=True if user.password==password: return redirect(url_for('shouye')) else: return '密碼錯誤' else: return '此用戶不存在' @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('shouye')) def login_re(func): #參數是函數 @wraps(func) def wrapper(*args, ** kwargs): #定義個函數將其返回 if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('denglu')) return wrapper #返回一個函數 @app.route('/question/',methods=['GET','POST']) @login_re def question(): if request.method == 'GET': return render_template('question.html') else: title = request.form.get('title') detail = request.form.get('detail') authorID = User.query.filter(User.username == session.get('user')).first().id question = Question(title=title,detail=detail,authorID=authorID) db.session.add(question) db.session.commit() return redirect(url_for(('shouye'))) @app.route('/comment/',methods=['GET','POST']) @login_re def comment(): detail = request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id question_id = request.form.get('question_id') comment = Comment(author_id=author_id,question_id=question_id,detail=detail) db.session.add(comment) db.session.commit() return redirect(url_for('question_detail',question_id=question_id)) @app.route('/self/<user_id>') def self(user_id): user = User.query.filter(User.id==user_id).first() context={ 'user':user } return render_template('self.html',**context) if __name__ == '__main__': app.run(debug=True)
html:css
{% extends'ba.html' %} {% block title %} {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/self.css')}}" type="text/css"> {% endblock %} {% block main %} <body> <div class="detail"> <h2>{{ user.username }}</h2> <div class="all questions"> <p class="p">所有問答</p> <div class="detail_left"> {% for foo in user.question %} <span class="icon2" aria-hidden="true"></span> <a href="#" class="name">{{ foo.author.username }}</a> <span class="badge">{{ foo.creatTime }}</span> <br> <p class="title">{{ foo.title }}</p> <p class="wenzhang" >{{ foo.detail }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> {% endfor %} </div> </div> <div class="all comments"> <p class="p">所有評論</p> <div class="detail_left"> {% for foo in user.comments %} <span class="icon2" aria-hidden="true"></span> <a href="#" class="name">{{ foo.author.username }}</a> <span class="badge">{{ foo.creatTime }}</span> <br> <p class="neirong">{{ foo.detail }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> {% endfor %} </div> </div> <div class="self"> <p class="p">我的信息</p> <div class="detail_left"> <li>用戶名:{{ user.username }}</li> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> <li>編號:{{ user.id }}</li> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> <li>文章篇數:{{ user.question|length }}</li> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> </div> </div> </div> </body> {% endblock %}