主页功能主要以数据渲染和维持回话功能为主。这里只举例几个使用语法,不详细cp所有主页内容
一、主页面
略
<ul class="dropdown-menu">
% if super=='False':
<li><a href="/addmedia">添加多媒体</a></li>
<li role="separator" class="divider"></li>
<li><a href="/logout">退出登录</a></li>
% else:
<li><a href="/manage">后台管理</a></li>
<li><a href="/logout">退出登录</a></li>
% end
</ul>
<marquee scrolldelay="300" behavior="" direction="up" onmouseover="this.stop();" onmouseout="this.start();">
% for sn in snews:
<a href="#" class="list-group-item btn-warning">
<h4 class="list-group-item-heading"作者:{{ sn.author }}</h4>
<p class="list-group-item-text">标题:{{ sn.title }}</p>
<img width="90%" src="{{ sn.content }}" alt="">
</a>
%end
</marquee>
略
二、数据库设计
这里举例一个模型
from sqlalchemy import Column, Integer, Sequence, String, Boolean, Enum
from utils.database import Base
class Snews(Base):
__tablename__ = 'snews'
id = Column(Integer, Sequence('id_seq'), primary_key=True, autoincrement=True)
author = Column(String(70))
title = Column(String(70))
content = Column(String(2000))
def __init__(self, author,title, content):
self.author = author
self.title = title
self.content = content
def __repr__(self):
return "<Snews('%s')>" % self.title
三、主页面视图
from bottle import template, request, redirect, response
from bottle import route, abort
from .models import Users, Notice, Snews, Wnews, Tnews, Music, Video, Sideo
from utils.database import session
# 主页面视图
@route("/")
def index():
if request.get_cookie('account'):
# set_cookie怎么设置,这里就怎么解,获取'account'对应的值
nickname = request.get_cookie('account', secret='123')
username = session.query(Users).filter_by(nickname=nickname).first().username
super = str(session.query(Users).filter_by(nickname=nickname).first().is_superuser)
notice = session.query(Notice).filter_by().order_by(Notice.id.desc()).first()
print(session.query(Notice).filter_by().first().title)
snews = session.query(Snews).all()
wnews = session.query(Wnews).all()
tnews = session.query(Tnews).all()
music = session.query(Music).filter_by().order_by(Music.id.desc())
video = session.query(Video).filter_by().order_by(Video.id.desc())
sideo = session.query(Sideo).filter_by().order_by(Sideo.id.desc())
data = dict(username=nickname, super=super, notice=notice, snews=snews, wnews=wnews, tnews=tnews, music=music, video=video, sideo=sideo)
return template('index.html', title='首页', **data)
return redirect('/login')
四、模板介绍
显而易见,我们在主页主要使用了sqlalchemy的数据查询功能和自带模板的使用。个人觉得自带模板的使用还是比较优秀的。但是考虑到使用Django自带模板和flask使用的jinjia2模板的习惯。所以这里给大家隆重介绍bottle更优秀的地方,它也支持jinjia2
1、自带模板变量
% for sn in snews:
<a href="#" class="list-group-item btn-warning">
<h4 class="list-group-item-heading"作者:{{ sn.author }}</h4>
<p class="list-group-item-text">标题:{{ sn.title }}</p>
<img width="90%" src="{{ sn.content }}" alt="">
</a>
%end
2、自带模板遍历
% for sn in snews:
<a href="#" class="list-group-item btn-warning">
<h4 class="list-group-item-heading"作者:{{ sn.author }}</h4>
<p class="list-group-item-text">标题:{{ sn.title }}</p>
<img width="90%" src="{{ sn.content }}" alt="">
</a>
%end
3、自带模板判断
% if super=='False':
<div class="jumbotron">
<h1 style="color: #b92c28">{{ notice.title }}</h1>
<p>{{ notice.content }}</p>
</div>
% else:
<div class="jumbotron">
<h1 style="color: #b92c28">{{ notice.title }}</h1>
<p>{{ notice.content }}</p>
</div>
% end
4、函数
①include(sub_template, **variables)
# 导入其他模板文件
% include('header.html', title='Page Title')
Page Content
% include('footer.tpl')
②rebase(name, **variables)
base.html(公共模板)
<html>
<head>
<title>{{title or 'No title'}}</title>
</head>
<body>
{{!base}} /*!为转义作用*/
</body>
</html>
index.html
# 导入母版
% rebase('base.tpl', title='Page Title')
<p>Page Content ...</p>
但是在磨板试用的灵活性上来说,自带模板差了好多,所以这里引出jinjia2。下面是源码里描述的三个模板接口,当然也需要你自己下载jinjia2第三方库
pip install jinjia2
使用方法:
from bottle import jinja2_template
@route('/login')
def login():
return jinja2_template('login.html')
至于它的具体模板语法,没有什么可讲的。详细参考:https://palletsprojects.com/p/jinja/
基础模板参考博客:https://blog.csdn.net/m0_37886429/article/details/81978120