标签:process 分享 查找 register incr ado method 运动员 containe
1. 在首页添加显示问答的列表,并定义好相应的样式。
无序列表
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
2. 用字典向index.html传递参数
<!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> <p>{{ username }}contextx</p> <div class="list-container"> <ul class="news-list"> {# <li style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 0px 0px;">#} {# <a href="#">username#} {# <div class="news-list-thumb"><img src=""></div>#} {# <div class="news-list-text">#} {# <div class="news-list-title" style="">学校领导接见空手道队</div>#} {# <div class="news-list-description">11月23日晚,学校领导接见载誉归来的空手道队,并与全体运动员和教练员座谈。</div>#} {# <div class="news-list-info">2017-11-24 学校综合办</div>#} {# <p>detail{{ questions }}</p>#} {# </div>#} {# </a>#} {# </li>#} <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> <a href="#">title</a> <span class="badgs">creat_time</span> <p>detail{{ questions }}</p> </li> </ul> </div>
* { font-family: "微软雅黑",Microsoft YaHei,"Helvetica Neue",Helvetica,Arial,sans-serif; } body{ font-size: 14px; line-height: 1.42857143; color: #333; background-color: #fff; } .list-container { width: 700px; margin-bottom: 40px; background-color: #dddddd; height: 500px; position:absolute; top:42%; left:50%; margin-top: -160px; margin-left:-300px; } .news-list { width: 100%; } .news-list a { display: block; } a:hover { color:#337ab7; text-decoration:none; } .news-list li .news-list-thumb { float: left; width: 30%; } .news-list li .news-list-text { float: left; width: 67%; padding-left: 2%; } .news-list li .news-list-text .news-list-title { font-size: 1.3em; } .news-list li .news-list-text .news-list-description { font-size: 1.1em; color: #777; text-indent: 2em; padding-top: 10px; padding-bottom: 10px; } .news-list li .news-list-text .news-list-info { color: #999; } li{ width:600px; height: 150px; border: 1px solid blanchedalmond; list-style-type: none; }
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‘:‘python20171201‘ } 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(‘/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‘)) if __name__ == ‘__main__‘: app.run(debug=True)
标签:process 分享 查找 register incr ado method 运动员 containe
原文地址:http://www.cnblogs.com/wk15/p/7941311.html