标签:war error question roc html add sheet 一个 efault
1. 在首页添加显示问答的列表,并定义好相应的样式。
无序列表
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
2. 用字典向index.html传递参数。
first.html
{% extends ‘base.html‘ %} {% block title %} 首页* {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/first.css‘) }}"> {% endblock %} {% block main %} <p>{{username}}</p> <ul class="list-group"> <li class="1"> <span class="1-1" aria-hidden="true"></span> <a href="#">username</a> <br> <a href="#">title</a> <span class="1-2">creat_time</span> <p>detail {{questions}}</p> </li> </ul> {% endblock %}
import config from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy 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(20),nullable=False) password = db.Column(db.String(20),nullable=False) class Question(db.Model): __tablename__ = ‘question‘ id = db.Column(db.Integer, primary_key=True, autoincrement=True) question = db.Column(db.String(100), nullable=False) questionDetail = db.Column(db.Text, nullable=False) creat_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 first(): context = { ‘questions‘:‘python20171201‘ } return render_template(‘first.html‘,**context) @app.route(‘/sign_up/‘,methods=[‘GET‘,‘POST‘])#登录 def sign_up(): if request.method == "GET": return render_template("sign_up.html") else: username = request.form.get("username") password = request.form.get("password") user = User.query.filter(User.username == username).first() if user: if user.password == password: session[‘user‘] = username sessionpermanent = True return redirect(url_for(‘first‘)) else: return ‘password error‘ else: return ‘username is not existed‘ @app.route(‘/sign_in/‘,methods=[‘GET‘,‘POST‘])#注册 def sign_in(): if request.method == "GET": return render_template("sign_in.html") else: username = request.form.get("username") password = request.form.get("password") user = User.query.filter(User.username == username).first() if user: return ‘username existed.‘ else: user = User(username=username, password=password) db.session.add(user) db.session.commit() return redirect(url_for(‘first‘)) @app.context_processor def mycontext(): usern=session.get(‘user‘) if usern: return {‘username‘:usern} else: return {} @app.route(‘/logout/‘) def logout(): session.clear() return render_template("first.html") def log(func): #参数是函数 @wraps(func) def wrapper(*args, **kwargs): #定义个函数将其返回 if session.get(‘user‘): return func(*args, **kwargs) else: return redirect(url_for(‘sign_up‘)) return wrapper #返回一个函数 @app.route(‘/question/‘, methods=[‘GET‘ , ‘POST‘]) @log def question(): if request.method == ‘GET‘: return render_template(‘question.html‘) else: question = request.form.get(‘question‘) questionDetail = request.form.get(‘questionDetail‘) author_id = User.query.filter(User.username == session.get(‘user‘)).first().id question = Question(question = question, questionDetail = questionDetail, author_id = author_id) db.session.add(question) db.session.commit() return redirect(url_for(‘first‘)) if __name__ == ‘__main__‘: app.run(debug=True)
css
*{ color:silver; } .list-group{ border: solid 2px silver; font-size:30px }
标签:war error question roc html add sheet 一个 efault
原文地址:http://www.cnblogs.com/lyx1997/p/7941272.html