from flask import Flask,render_template,request,redirect,url_for,session
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
from functools import wraps
from datetime import datetime
app = Flask(__name__)
#app.config['SECRET_KEY'] = '1234567'
app.config.from_object(config)
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'user_test'
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)
nickname = db.Column(db.String(20))
tel = db.Column(db.String(20))
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)
creat_time = db.Column(db.DateTime,default=datetime.now)
author_id = db.Column(db.Integer,db.ForeignKey('user_test.id'))
author = db.relationship('User',backref=db.backref('question'))
# db.create_all()
#2.在表中插入一条记录
#user = User(username = 'mis1',password = '124', nickname='wk')
#db.session.add(user)
#db.session.commit()
#3.去表中查询用户名对应的密码和接下来用户输入的密码是否相匹配
#user = User.query.filter(User.username == 'mis1234').first()
#print(user)
#print(user.id,user.username)#输出查找到的用户名的id,密码等.
#修改查询到的用户名的密码等
#user = User.query.filter(User.username == 'mis1234').first()
#user.password = '123'
#db.session.commit()
#再次插入一条记录
# user = User(username = 'wkkkkkk',password = '123456', nickname='kk')
# db.session.add(user)
# db.session.commit()
#删除一条记录
#user = User.query.filter(User.username == 'mis1234').first()
#db.session.delete(user)
#db.session.commit()
@app.route('/')
def index():
context={
'questions':Question.query.all()
}
return render_template('index.html',**context)
@app.route('/detail/<question_id>')
def detail(question_id):
return render_template('detail.html',ques = question_id)
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
usern = request.form.get('uname')
passw = request.form.get('upass')
user = User.query.filter(User.username==usern).first()
if user:
if user.password == passw:
session['user'] = usern;
return redirect(url_for('index'))
else:
return 'password error!'
else:
return 'username is not exister!'
@app.route('/register/',methods=['GET','POST'])
def register():
if request.method =='GET':
return render_template('register.html')
else:
usern = request.form.get('uname')
teln = request.form.get('number')
passw = request.form.get('upass')
user = User.query.filter(User.username==usern).first()
if user:
return 'username existed'
else:
user1= User(username=usern,tel=teln,password=passw)
db.session.add(user1)
db.session.commit()
return redirect(url_for('login'))
@app.context_processor
def mycontext():
usern = session.get('user')
if usern:
return{'username':usern}
else:
return {}
def loginFirst(func):
@wraps(func)
def wrapper(*args,** kwargs):
if session.get('user'):
return func(*args,** kwargs)
else:
return redirect(url_for('login'))
return wrapper
@app.route('/logout/')
def logout():
session.clear()
return redirect(url_for('index'))
@app.route('/question/',methods=['GET','POST'])
@loginFirst
def question():
if request.method =='GET':
return render_template('question.html')
else:
biaoti = request.form.get('biaoti')
detail = request.form.get('detail')
author_id = User.query.filter(User.username==session.get('user')).first().id
question = Question(title=biaoti,detail=detail,author_id=author_id)
db.session.add(question)
db.session.commit()
return redirect(url_for('index'))
@app.route('/questionDetail/')
def questionDetail():
return render_template('questionDetail.html')
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/index.css')}}">
</head>
<body>
{#<a href="http://127.0.0.1:5000/login/">login</a>#}
{#<a href="{{ url_for('register')}}">register</a>#}
{#<a id="href" href="{{ url_for('question') }}">question</a>#}
<nav id="nav" >
<!--<img id="on_off" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" > -->
<img id="on_off" onclick="mySwitch()" src="{{ url_for('static',filename='image/switchon.gif') }}" >
<a id="href" href="">学生</a>
<a id="href" href="">教师</a>
<a id="href" href="">校友</a>
<input id="search" type="text" name="search">
<input id="button1" type="button" name="sousuo" value="搜索">
<!--<input onclick="window.location.replace('login.html')" id="button" type="button" name="login" value="登录">
<input onclick="window.location.replace('register.html')" id="button" type="button" name="login" value="注册"><br>-->
<!--<a id="href" href="{{ url_for('login') }}">login</a>
<a id="href" href="{{ url_for('register') }}">register</a>
<a id="href" href="{{ url_for('question') }}">question</a> -->
{% if username %}
<a id="href" href="#">{{ username }}</a>
<a id="href" href="{{ url_for('logout')}}">logout</a>
<a id="href" href="{{ url_for('question') }}">question</a>
<a id="href" href="{{ url_for('index') }}">index</a>
{% else %}
<a id="href" href="{{ url_for('login') }}">login</a>
<a id="href" href="{{ url_for('register') }}">register</a>
<a id="href" href="{{ url_for('question') }}">question</a>
{% endif %}
</nav>
<p>{{ username }}context</p>
<div class="list-container">
<ul class="news-list">
{% for foo in questions %}
<li style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 0px 0px;">
<span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
<a href="#">{{ foo.author.username }}</a>
<br>
<a href="#" onclick="window.location.replace({{ url_for("questionDetail") }})">{{ foo.title }}</a>
<span class="badgs">{{ foo.creat_time }}</span>
<p>{{ foo.detail}}</p>
</li>
{% endfor %}
</ul>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>问答详情页</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/questionDetail.css')}}">
</head>
<body>
{#<a href="http://127.0.0.1:5000/login/">login</a>#}
{#<a href="{{ url_for('register')}}">register</a>#}
{#<a id="href" href="{{ url_for('question') }}">question</a>#}
<nav id="nav" >
<!--<img id="on_off" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" > -->
<img id="on_off" onclick="mySwitch()" src="{{ url_for('static',filename='image/switchon.gif') }}" >
<a id="href" href="">学生</a>
<a id="href" href="">教师</a>
<a id="href" href="">校友</a>
<input id="search" type="text" name="search">
<input id="button1" type="button" name="sousuo" value="搜索">
<!--<input onclick="window.location.replace('login.html')" id="button" type="button" name="login" value="登录">
<input onclick="window.location.replace('register.html')" id="button" type="button" name="login" value="注册"><br>-->
<!--<a id="href" href="{{ url_for('login') }}">login</a>
<a id="href" href="{{ url_for('register') }}">register</a>
<a id="href" href="{{ url_for('question') }}">question</a> -->
{% if username %}
<a id="href" href="#">{{ username }}</a>
<a id="href" href="{{ url_for('logout')}}">logout</a>
<a id="href" href="{{ url_for('question') }}">question</a>
<a id="href" href="{{ url_for('index') }}">index</a>
{% else %}
<a id="href" href="{{ url_for('login') }}">login</a>
<a id="href" href="{{ url_for('register') }}">register</a>
<a id="href" href="{{ url_for('question') }}">question</a>
{% endif %}
</nav>
<p>{{ username }}context</p>
<div class="list-container">
<div class="title_author_time">
<a href="#" id="title">title</a>
<p>{{ username }}
<a href="#" id="time">creat_time</a>
</div>
<div class="title">
<a href="#" id="title">title</a>
</div>
<div class="pinglun">
<label for="pinglun" ></label>
<textarea class="form-control" rows="5" id="pinglun" name="pinglun" ></textarea>
<button type="submit" class="btn bun-default">发送</button>
</div>
<div class="pinglunD">
评论:
<ul class="news-list">
<li style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 0px 0px;">
<span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
<a href="#">username</a>
<br>
<p>detail</p>
</li>
</ul>
</div>
{# <ul class="news-list">#}
{# {% for foo in questions %}#}
{# <li style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 0px 0px;">#}
{# <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>#}
{# <a href="#">{{ foo.author.username }}</a>#}
{# <br>#}
{# <a href="#">{{ foo.title }}</a>#}
{# <span class="badgs">{{ foo.creat_time }}</span>#}
{# <p>{{ foo.detail}}</p>#}
{# </li>#}
{# {% endfor %}#}
{# </ul>#}
</div>