制作首页的显示列表

Posted 李海力

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了制作首页的显示列表相关的知识,希望对你有一定的参考价值。

在首页添加显示问答的列表,并定义好相应的样式。

无序列表

<ul >

  <li>Coffee</li>

  <li>Tea</li>

  <li>Milk</li>

</ul>

用字典向index.html传递参数。

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):
    __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)
    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 daohang():
    context={
        ‘user‘:‘chen‘
    }
    return render_template(‘index.html‘,**context)
 
 
@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, User.password == password).first()
        if user:
            session[‘user‘] = username
            session.permanent = True
            return redirect(url_for(‘daohang‘))
        else:
            return ‘error username or password‘
 
 
 
@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(‘regist‘))
 
 
@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‘)
        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(‘login‘))
 
 
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(‘daohang‘))
 
if __name__ == ‘__main__‘:
    app.run(debug=True)

2. 用字典向index.html传递参数。

{% extends "daohang.html" %}
{% block title %}首页{% endblock %}
 
{% block main %}
   <p>{{ user }}contextx</p>
   <ul class="list-group" style="...">
 
       <li class="list-group-item">
           <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
           <a href="#">{{ user }}</a>
           <br>
           <a href="#">标题</a>
           <span class="badge">发布时间</span>
           <p style="...">内容</p>
 
       </li>

 

以上是关于制作首页的显示列表的主要内容,如果未能解决你的问题,请参考以下文章

制作首页的显示列表。

制作首页的显示列表

制作首页的显示列表

制作首页的显示列表

制作首页的显示列表

制作首页的显示列表