製做首頁的顯示列表。

1. 在首頁添加顯示問答的列表,並定義好相應的樣式。css

無序列表html

<ul >python

  <li>Coffee</li>sql

  <li>Tea</li>flask

  <li>Milk</li>session

</ul>app

2. 用字典向index.html傳遞參數。url

{% extends'base.html' %}
{% block title %}
    首頁
{% endblock %}
{% block head %}
    <link rel="stylesheet" href="{{ url_for('static',filename='css/shouye.css')}}" type="text/css">
{% endblock %}
{% block main %}
<body>
<div class="content">
<ul class="list-group">
    <li class="list-group-item">
        <span class="glyphicon-leaf" aria-hidden="true"></span>
        <a href = "">username</a>
    <br>
        <a href = "">title</a>
               <span class="badge">create_time</span>
        <p style="text-align: left">detail{{ question }}</p>
</li>

</ul>
</div>
</body>

</html>
{% endblock %}
from flask import Flask,redirect,request,url_for,session
from flask import render_template
from flask_sqlalchemy import SQLAlchemy
from functools import wraps
from datetime import  datetime
import config

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)
    nickname=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)
    create_time=db.Column(db.DateTime,default=datetime.now)
    author_id=db.Column(db.Integer,db.ForeignKey('user.id'))
    anthor=db.relationship('User',backref=db.backref('question'))
db.create_all()
#查詢
# user=User.query.filter(User.username=='mis1234').first()
# print(user.id,user.username,user.password)

#增長
# user=User(username='mis1234',password='111111',nickname='l')
# db.session.add(user)
# db.session.commit()

#增長
# user=User(username='luk',password='123456',nickname='lu')
# db.session.add(user)
# db.session.commit()
# 修改
# user=User.query.filter(User.username=='mis1234').first()
# user.password='12345678'
# db.session.commit()
#
# # 刪除
# user=User.query.filter(User.username=='mis1234').first()
# db.session.delete(user)
# db.session.commit()


@app.route('/')
def base():
    return render_template('base.html')

@app.route('/shouye')
def shouye():
    context={
        'question':'python20171001'
    }
    return render_template('shouye.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('username')
        passw = request.form.get('password')
        user = User.query.filter(User.username == usern).first()
        if user:
            if user.password==passw:
                session['user']=usern
                return redirect(url_for('shouye'))
            else:
                 return '密碼錯誤。'
        else:
            return  '用戶名不存在。'

@app.route('/register/',methods=['GET','POST'])
def register():
    if request.method=='GET':
        return render_template('register.html')
    else:
        usern=request.form.get('username')
        nickn = request.form.get('nickname')
        passw = request.form.get('password')
        user=User.query.filter(User.username==usern).first()
        if user:
            return '用戶名已存在。'
        else:
            user1=User(username=usern,password=passw,nickname=nickn)
            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 {}

@app.route('/logout/')
def logout():
    session.clear()
    return redirect(url_for('shouye'))

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:
        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)
        # question.anthor=user
        db.session.add(question)
        db.session.commit()
    return redirect(url_for('shouye'))
# def question():
#     return render_template('question.html')

# @app.route('/shouye/')
# def shouye():
#     return render_template('shouye.html')

if __name__ == '__main__':
    app.run(debug=True)

相關文章
相關標籤/搜索