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

制作首页的显示列表。

时间:2017-12-02 23:16:32      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:login   问答   ges   .gz   cto   bsp   _for   icon   return   

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

无序列表

<ul >

  <li>Coffee</li>

  <li>Tea</li>

  <li>Milk</li>

</ul>

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

{% extendsbase.html %}
    {% block title %}
    首页
    {% endblock %}
{% block head %}
    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/shouye.css‘) }}">
{% endblock %}
{% block main %}
<body>


<ul class="list-group">
            <li style="padding-left: 0px;padding-right: 10px;box-shadow: #cccccc" class="list-group-item">
                <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                <a href="#">{{ user }}</a><br>
                <a href="#" class="title">{{ title }}</a>
                <span class="badge">{{ time }}</span>
                <p class="detail">{{ detail }}</p>

            </li>
        </ul>

<div class="tu">
        <div class="img">
            <a href="http://www.gzcc.cn/">
                <img src="http://static.runoob.com/images/demo/demo4.jpg"></a>
            <div class="desc"><a href="http://www.gzcc.cn">校园风光</a></div>
        </div>
        <div class="img">
            <a href="http://www.gzcc.cn/">
                <img src="http://static.runoob.com/images/demo/demo3.jpg"></a>
            <div class="desc"><a href="http://www.gzcc.cn">校友风采</a></div>
        </div>
        <div class="img">
            <a href="http://www.gzcc.cn/">
                <img src="http://static.runoob.com/images/demo/demo2.jpg"></a>
            <div class="desc"><a href="http://www.gzcc.cn">学校文化</a></div>
        </div>
        <div class="img">
            <a href="http://www.gzcc.cn/">
                <img src="http://static.runoob.com/images/demo/demo1.jpg"></a>
            <div class="desc"><a href="http://www.gzcc.cn">学生风采</a></div>
        </div>
    </div>

</body>

{% endblock %}
from flask import Flask,render_template,request,redirect,url_for,session
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)


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(/shouye/)
def shouye():
    context = {
        user: ‘‘,
        title: ‘‘,
        time: ‘‘,
        detail: ‘‘
    }
    return render_template(shouye.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
                return redirect(url_for(shouye))
            else:
                return upassword error
        else:
            return uusername is not existed.


@app.route(/zhuce/,methods=[GET,POST])
def zhuce():
    if request.method == GET:
        return render_template(zhuce.html)
    else:
        usern = request.form.get(username)
        passw = request.form.get(password)
        user = User.query.filter(User.username == usern).first()
        if user:
            return uusername existed
        else:
            user1 = User(username=usern, password=passw)
            db.session.add(user1)
            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(shouye))



@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))



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

 

制作首页的显示列表。

标签:login   问答   ges   .gz   cto   bsp   _for   icon   return   

原文地址:http://www.cnblogs.com/xy123ing/p/7955814.html

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