py主文件代碼html
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) create_time = db.Column(db.DateTime, default=datetime.now ) author_id = 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')) create_time = db.Column(db.DateTime, default=datetime.now) detail = db.Column(db.Text, nullable=False) question = db.relationship('Question',backref=db.backref('comments')) author = db.relationship('User',backref=db.backref('comments')) db.create_all() # # 增長 # user = User(username = 'zhuyinyin',password='113428164508') # db.session.add(user) # db.session.commit() # 修改 # ser=User.query.filter(User.username =='zhuyinyin').first() # user.password='123' # print(user.username,user.password) # db.session.commit() #查詢: # User.query.filter(User.username == 'zhuyinyin').first() # print(user.username,user.password) # db.session.commit() # 修改: # user.password = '111111' # db.session.commit() # 刪除: # db.session.delete(user) # db.session.commit() @app.route('/') def hello_world(): return render_template('ba.html') @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,User.password==password).first() # #判斷用戶名是否存在 # if user: # if user.password==password: # return redirect(url_for('shouye')) # else: # return '密碼錯誤' # else: # return '用戶不存在' #第二種寫法 @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 log(func): #參數是函數 @wraps(func) def wrappers(*args,** kwargs): #定義個函數將其返回 if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('denglu')) return wrappers #返回一個函數 @app.route('/shouye/') def shouye(): context = { 'questions':Question.query.order_by('create_time').all() } return render_template('shouye.html',**context) @app.route('/detail/<question_id>') # 和idea的update同樣,將id帶到控制器 def detail(question_id): quest = Question.query.filter(Question.id==question_id).first()# 根據id查詢出整條元組記錄,丟進quest comments = Comment.query.filter(Comment.question_id==question_id).all() return render_template('detail.html',ques=quest,comments=comments)# 把值quest丟進鍵ques,comments丟進comments在fabuview.html頁面調用 @app.route('/comment/',methods=['POST']) @log def comment(): comment = request.form.get('new_comment') ques_id = request.form.get('question_id') auth_id=User.query.filter(User.username==session.get('user')).first().id comm=Comment(author_id=auth_id,question_id=ques_id,detail=comment) db.session.add(comm) db.session.commit() return redirect(url_for('detail',question_id=ques_id)) @app.route('/wenda/',methods=['GET','POST']) @log def wenda(): if request.method=='GET': return render_template('wenda.html') else: title=request.form.get('title') detail=request.form.get('detail') author_id=User.query.filter(User.username==session.get('user')).first().id question=Question(title=title,detail=detail,author_id=author_id) db.session.add(question)#保存到數據庫 db.session.commit()#提交 return redirect(url_for('shouye'))#跳轉 if __name__ == '__main__': app.run(debug=True)
詳情頁:前端
{% extends 'ba.html' %} {% block title %} 問答詳情 {% endblock %} {% block main %} <div class="box"> <h3 href="#">{{ ques.title }}</h3> <small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span> </small> <hr> <p>{{ ques.detail }}</p> <hr> <form action="{{ url_for('comment') }}" method="post"> <div><textarea class="form-control" id="new-comment" rows="3" style="margin-left: 1%" name="new_comment" placeholder="write your comment"></textarea><br></div> <input type="hidden" name="question_id" value="{{ ques.id }}"> <button type="submit">發送</button> </form> <h4>評論:({{ ques.comments|length }})</h4> <ul class="list-unstyled"> {% for foo in comments %} <li class="list-group-item"> <a>{{ foo.author.username }}</a> <span class="badge pull-right">{{ foo.create_time }}</span> <p>{{ foo.detail }}</p> <br> </li> {% endfor %} </ul> </div> {% endblock %}