@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)
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() @app.route('/') def hello_world(): return render_template('base.html') @app.route('/zhuce/',methods=['GET','POST']) def zhuce(): if request.method == 'GET': return render_template('zc.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('dl.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('hh.html',**context) @app.route('/wenda/',methods=['GET','POST']) @log def wenda(): if request.method=='GET': return render_template('wd.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'))#跳轉 @app.route('/detail/<question_id>') def detail(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template('detail.html',ques=quest) if __name__ == '__main__': app.run(debug=True)
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
創建評論的對象關係映射:css
class Comment(db.Model):
__tablename__='comment'html
嘗試實現發佈評論。sql
{% extends 'base.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> <div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment" placeholder="write your comment"></textarea><br></div> <button type="submit" >發送</button> </form> <h4>評論:</h4> <ul class="list-group"> <li class="list-group-item"> <img style="width: 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64"> <a href="#"></a><br> <p style="align-content: center"></p> <span class="badge" style="margin-left: 60%"></span> <p style="margin-left: 25%"></p><br> </li> </ul> </div> {% endblock %}