码迷,mamicode.com
首页 > 其他好文 > 详细

首页列表显示全部问答,完成问答详情页布局。

时间:2017-12-05 23:56:30      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:wrapper   hid   button   datetime   lte   context   foo   creat   idt   

  1. 首页列表显示全部问答:
    1. 将数据库查询结果传递到前端页面 Question.query.all()
    2. 前端页面循环显示整个列表。
    3. 问答排序
  2. 完成问答详情页布局:
    1. 包含问答的全部信息
    2. 评论区
    3. 以往评论列表显示区。
  3. 在首页点击问答标题,链接到相应详情页。
    {% extends base.html %}
    
    {% block title %}首页{% endblock %}
    
    {% block head %}
        <link href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" rel="stylesheet" type="text/css">
        <script src="{{ url_for(‘static‘,filename=‘js/index.js‘) }}"></script>
    {% endblock %}
    
    {% block main %}
    <p>{{ username }}contextx</p>
        <ul class="list-group"style="">
        {% for foo in questions %}
                <li class="list-group-item">
                    <span class="glyphicon glyphicon-leaf"aria-hidden="true"></span>
                    <a href="#">{{ foo.author.username }}</a>
                    <br>
                    <a href="*">{{ foo.title }}</a>
                    <span class="badge">{{ foo.creat_time }}</span>
                    <p style="text-indent:18px">{{ foo.detail }}</p>
                </li>
    
        </ul>
    
    
    
    
    <body background="http://f10.baidu.com/it/u=4181379019,968220141&fm=72"></body>
    
    {% endblock %}
    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):
        __table__name = 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(50))
    db.create_all()
    
    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.id))
        author = db.relationship(User, backref=db.backref(question))
    
    db.create_all()
    
    
    @app.route(/)
    def base():
        return render_template(base.html)
    
    @app.route(/index/)
    def index():
        context = {
                question:Question.query.all()
            }
        return render_template(index.html,**context)
    
    @app.route(/detail)
    def detail():
        return render_template("detail.html")
    
    @app.route(/regist/,methods=[GET,POST])
    def regist():
        if request.method==GET:
            return render_template(regist.html)
        else:
            username=request.form.get(username)
            password=request.form.get(password)
            nickname=request.form.get(nickname)
            user=User.query.filter(User.username==username).first()
            if user:
                return uusername existed
            else:
                user=User (username=username,password=password,nickname=nickname)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for(login))
    
    
    
    @app.route(/login/,methods=[GET,POST])
    def login():
        if request.method==GET:
            return render_template(login.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
                    return redirect(url_for(index))
                else:
                    return upassword error
            else:
                return uusername is not existed
    
    
    @app.route(/logout/)
    def logout():
        session.clear()
        return redirect(url_for(index))
    
    
    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)
            db.session.add(question)
            db.session.commit()
            return redirect(url_for(index))
    
    
    
    @app.context_processor
    def mycontext():
        usern=session.get(user)
        if usern:
            return{username:usern}
        else:
            return{}
    
    
    if __name__ == __main__:
        app.run(debug=True)
    {% extends base.html %}
    
    {% block title %}
    反馈中心
    {% endblock %}
    
    {% block main %}
        <div class="divtouming">
        <div class="panel panel-default" style="width: 800px">
            <div class="panel-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="fankuititle" class="col-sm-2 control-label">反馈原因</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="fankuititle">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="fankuitext" class="col-sm-2 control-label">详情</label>
                        <div class="col-sm-10">
                            <textarea id="fankuitext" rows="5" cols="87"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">提交</button>
                        </div>
                    </div>
                </form>
            </div>
        </div></div>
    {% endblock %}

     

首页列表显示全部问答,完成问答详情页布局。

标签:wrapper   hid   button   datetime   lte   context   foo   creat   idt   

原文地址:http://www.cnblogs.com/acef/p/7990041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!