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 log(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'])
#@loginFirst
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'])
#@loginFirst
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))
if __name__ == '__main__':
app.run(debug=True)
{% extends'ba.html' %}
{% block title %}
详情
{% endblock %}
{% block head %}
<link rel="stylesheet" href="{{ url_for('static',filename='css/detail.css')}}" type="text/css">
{% endblock %}
{% block main %}
<body>
<div class="detail">
<div class="detail_left">
<h2 align="center">{{ question.title }}</h2>
<a class="username">{{ question.author.username }}</a>
<span class="badge">{{ question.creatTime }}</span>
<hr>
<a style="white-space: pre-wrap" >{{ question.detail }}</a>
<hr>
<form action="{{ url_for('comment') }}" method="post">
<textarea name='detail' class="form-control" rows="6" id="questionDetail">
</textarea>
<br> <button class="btn-default">发布</button>
<input name="question_id" value="{{ question.id }}" type="hidden" />
</form>
<ul>
<p style="font-weight: 600;font-size: 16.5px">评论:[{{ question.comments|length }}]</p>
<div>
{% for foo in question.comments %}
<li>
<a href="#">{{ foo.author.username }}</a>
<span>{{ foo.creat_time }}</span><br>
<p>{{ foo.detail }}</p>
</li>
{% endfor %}
</div>
</ul>
</div>
</div>
</body>
{% endblock %}