标签:错误 密码 tab main href sql static lis redirect
1. 在首页添加显示问答的列表,并定义好相应的样式。
无序列表
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
2. 用字典向index.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‘)) # db.create_all() @app.route(‘/‘) def base(): return render_template(‘base.html‘) @app.route(‘/zc/‘,methods=[‘GET‘,‘POST‘]) def zc(): if request.method == ‘GET‘: return render_template(‘zc.html‘) else: username = request.form.get(‘username‘) # 获取form中的数据 password = request.form.get(‘password‘) # 获取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(‘/dl/‘,methods=[‘GET‘,‘POST‘]) def dl(): 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(‘/hh/‘) def hh(): context = { ‘title‘:‘论盟主的死忠粉是怎样炼成的‘, ‘detail‘:‘始于颜值,陷于才华,忠于人品?最重要的当然是颜值啦!‘, } return render_template(‘hh.html‘,**context) @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(‘hh‘))#跳转 if __name__ == ‘__main__‘: app.run(debug=True)
{% extends‘base.html‘ %} {% block title %} 首页 {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for(‘static‘,filename=‘css/hh.css‘)}}" type="text/css"> {% endblock %} {% block main %} <br><br><br> <div class="box"> <ul class="list-group"> <li class="list-group-item"> <a href="#">{{ username }}</a><br> <a href="#">问题:{{ title }}</a><br> <a href="#">详情:{{ detail }}</a> <span class="badge" style="margin-left: 60%">{{ create_time }}发布时间</span> </li> <br> </ul> </div> <div class="all"> <div class="img"> <a href=""> <img src="http://img2.touxiang.cn/file/20160601/78050c8c02997f95d03bc5c8041b5131.jpg"></a> <div class="wenzi1"><a href="">一念无明</a></div> </div> <div class="img"> <a href=""> <img src="http://img2.touxiang.cn/file/20160601/9144c9b9c9bc63150d9aa5aac727b392.jpg"></a> <div class="wenzi2"><a href="">美人为馅</a></div> </div> <div class="img"> <a href=""> <img src="http://img2.touxiang.cn/file/20160504/b1e6d1af8b96f4506d4a8acc9a1182f7.jpg"></a> <div class="wenzi3"><a href="">公子如玉</a></div> </div> <div class="img"> <a href=""> <img src="http://img2.touxiang.cn/file/20160601/99473e3887d3e8c6e8a9015e134c3307.jpg"></a> <div class="wenzi4"><a href="">刀剑无眼</a></div> </div> </div> {% endblock %}
标签:错误 密码 tab main href sql static lis redirect
原文地址:http://www.cnblogs.com/00js/p/7944502.html