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

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

时间:2017-12-06 14:23:47      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:jpg   ==   sso   incr   wrap   tools   alt   integer   temp   

  1. 首页列表显示全部问答:
    1. 将数据库查询结果传递到前端页面 Question.query.all()
    2. 前端页面循环显示整个列表。
    3. 问答排序
  2. 完成问答详情页布局:
    1. 包含问答的全部信息
    2. 评论区
    3. 以往评论列表显示区。
  3. 在首页点击问答标题,链接到相应详情页。
    {% extends ‘basic.html‘ %}
    {% block title %}登陆{% endblock %}
    
    {% block head %}
        <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘../staticcss/20.css‘) }}">
        <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘../staticcss/200.css‘) }}">
        <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘../static/css/2000.css‘) }}">
        <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘../static/css/2000.css‘) }}">
        <script src="{{url_for(‘static‘,filename=‘../static/js/39.js‘) }}" type="text/css"></script>
    {% endblock %}
    
    {% block index %}
    </head>
    <body class="abc">
    
    <p>{{ user }}context</p>
    <ul class="list-group">
        {%  for foo in question %}
        <li class="list-group-item">
            <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
            <a href="#">{{ foo.author.username }}</a>
            <br>
            <a href="#"title>{{ foo.title }}</a>
            <br>
            <span class="badge">{{ foo.creat_time }}</span>
            <p style="text-indent: 18px">{{ foo.detail }}</p>
        </li>
    </ul>
        {% endfor %}
    
    
    
    <div>
    
        <div class="img">
            <a href={{ url_for(‘login‘) }}>
                <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4071790924,2914289697&fm=27&gp=0.jpg">
            </a>
            <div class="desc"><a href={{ url_for(‘login‘) }}>登陆</a> </div>
        </div>
         <div class="img">
            <a href={{ url_for(‘rege‘) }}>
                <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508478993164&di=21005bc9d7cbc500390e5b62c3b859a7&imgtype=0&src=http%3A%2F%2Fwapfile.desktx.com%2Fpc%2F161122%2Fbigpic%2F5832b76c05a7e.jpg">
            </a>
            <div class="desc"><a href={{ url_for(‘rege‘) }}>注册</a> </div>
        </div>
    
    </div>
    
    
    <div class="clearfloat">
    
    
    <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4071790924,2914289697&fm=27&gp=0.jpg">
    <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508478993164&di=21005bc9d7cbc500390e5b62c3b859a7&imgtype=0&src=http%3A%2F%2Fwapfile.desktx.com%2Fpc%2F161122%2Fbigpic%2F5832b76c05a7e.jpg">
    
    </div>
    
    
    
    </body>
    </html>{% endblock %}
    from flask_sqlalchemy import SQLAlchemy
    import config
    from flask import Flask,render_template,request,redirect, url_for,session
    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)
        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)
         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 index():
        context = {
            question:Question.query.all()
        }
        return render_template(index.html,**context)
    
    @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
                    session.permanent = True
                    return redirect(url_for(index))
                else:
                    return upassword error
            else:
                return upassword is not existed
    
    @app.route(/rege/,methods=[GET,POST])
    def rege():
        if request.method ==GET:
            return render_template(rege.html)
        else:
            usern = request.form.get(username)
            passw = request.form.get(password)
            nickn = request.form.get(nickname)
            user = User.query.filter(User.username ==usern).first()
            if user:
                return username.existed.
            else:
                user1 = User(username=usern,password=passw,nickname = nickn)
                db.session.add(user1)
                db.session.commit()
                return redirect(url_for(login))
    
    def loginFrist(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])
    @loginFrist
    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{}
    
    @app.route(/logout/)
    def logout():
        session.clear()
        return redirect(url_for(index))
    
    
    if __name__=="__main__":
        app.run(debug=True)

    技术分享图片

     

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

标签:jpg   ==   sso   incr   wrap   tools   alt   integer   temp   

原文地址:http://www.cnblogs.com/dujiacheng789/p/7992205.html

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