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


    1.首页列表显示全部问答:

    1. 将数据库查询结果传递到前端页面 Question.query.all()
    2. 前端页面循环显示整个列表。
    3. 问答排序

    2.完成问答详情页布局:

    1. 包含问答的全部信息
    2. 评论区
    3. 以往评论列表显示区。

    3.在首页点击问答标题,链接到相应详情页。

    shouye.html

    {% extends'base.html' %}
        {% block title %}
        首页
        {% endblock %}
    {% block head %}
    
        <script src="{{url_for('static',filename='js/base.js') }}" type="text/javascript"></script>
        <link href="{{ url_for('static' ,filename='css/base.css') }}"  rel="stylesheet" type="text/css">
        {% endblock %}
        {% block main %}
        {% for foo in question %}
    <body id="myBody">
            <div id="gufeng">
           <ul class="list" >
                 <li>用户名:{{ foo.author.username }}</li>
              <a href="{{ url_for("detail") }}">标题:{{ foo.title }}</a>
               <li class="">问题:{{ foo.detail }}</li>
               <li class="">时间:{{ foo.time }}</li>
           </ul>
            </div>
        {% endfor %}
        <footer>
            <div class="footer_box">
                Copyright@2017-2027 个人版权,版权所有 作者:JZX telephone:0000-1234567 mobile phone:11111111111
            </div>
        </footer>
    {% endblock %}

     xiangqing.html

    {% extends "base.html" %}
    {% block title%}问答详情{% endblock %}
    
    {% block main %}
    <body bgcolor="#ffd700">
        <div class="GuFeng">
          <h>标题</h><br>
            <li>username</li>
            <li>time</li>
            <hr>
            <li>标题</li>
            <hr>
            <textarea name='talk'></textarea>
            <br><button>发布</button>
            <p>评论:</p>
            <table border=2 >
                <tr><td>评论内容</td></tr>
                <tr><td>评论内容</td> </tr>
                <tr><td>评论内容</td></tr>
                </tr>
            </table>
        </div>
    </body>
    {% endblock %}

     py

    from flask import Flask,render_template,request,redirect,url_for,session
    from flask_sqlalchemy import SQLAlchemy
    import config
    from datetime import datetime
    from functools import wraps
    
    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))
    
    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('/detail/')
    def detail():
        return render_template('detail.html')
    
    @app.route('/zhuce/',methods=['GET','POST'])
    def zhuce():
        if request.method =='GET':
            return  render_template('zhuce.html')
        else:
            usern = request.form.get('name')
            teln = request.form.get('password')
            passw = request.form.get('password1')
            user = User.query.filter(User.username==usern).first()
            if user:
                return 'username existed'
            else:
                user= User(username=usern,tel=teln,password=passw)
                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 u'password error'
            else:
                return u'username is not existed'
    
    @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'))
    
    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'))
    if __name__ == '__main__':
        app.run(debug=True)
  • 相关阅读:

    es -Aggregations
    散列表学习
    SpringCloud (一)——基本的搭建
    1.5 中不支持 lambda 表达 java
    区块链 (一)——基础
    线性表
    Redis(四)——消息队列
    rune is alias of int32
    wall time
  • 原文地址:https://www.cnblogs.com/gdlyzx/p/7992093.html
Copyright © 2020-2023  润新知