一、具體實(shí)現(xiàn)步驟如下
1而芥、編寫相關(guān)處理請(qǐng)求的ProfileHandler類(在main.py中進(jìn)行實(shí)現(xiàn))
class ProfileHandler(BaseHandler):
"""
查看個(gè)人和各個(gè)用戶的上傳圖片和收藏的圖片
"""
@tornado.web.authenticated
def get(self):
username = self.get_argument('username', '')
user = self.orm.get_user(username)
username_current = self.current_user
user_id = self.db_session.query(User.id).filter_by(username=username).first()[0]
post_ids = self.db_session.query(LikePost.post_id).filter_by(user_id=user_id).all()
list_post = []
if post_ids:
for post_id in post_ids:
every_post = self.db_session.query(Post).filter_by(id=post_id[0]).first()
list_post.append(every_post)
else:
list_post = []
self.render('profile.html', user=user, like_post=list_post, username_current=username_current)
2可都、編寫前端代碼profile.html頁(yè)面
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../static/upload/hu.ico">
<title>個(gè)人上傳和收藏圖片界面</title>
<link rel="stylesheet">
<link href="../static/css/signin.css" rel="stylesheet">
</head>
<body style="background-color: darkslategrey">
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
{% if username_current!= None %}
<a class="navbar-brand" href="#">當(dāng)前在線用戶:<font style="color: yellow;font-size:20px">{{ username_current }}</font></a>
{% end %}
<a class="navbar-brand" href="#" style="margin-left: 250px"><font color="yellow">{{ user.username }}</font>共上傳<font style="color: yellow;font-size:20px">{{ len(user.posts) }}</font>張圖片</a>
<a class="navbar-brand" href="#" style="margin-left: 30px">共喜歡<font style="color: yellow;font-size:20px">{{ len(like_post) }}</font>張圖片</a>/
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li style="margin-right:40px "><a href="/logout"><font style="color: yellow;font-size:20px">退出</font></a></li>
</ul>
</div>
</div>
</nav>
<div class="container" style="margin-top: 15px;color: black;margin-left: 130px">
<h4><font color="yellow">{{ user.username }}</font>上傳圖片如下(用戶編號(hào)為{{ user.id }}):</h4>
{% for post in user.posts %}
<a href="/one_picture/{{ post.id }}"><img src="{{ static_url(post.image_url) }}" width="170" height="170"></a>
{% end %}<br>
<h4><font color="yellow">{{ user.username }}</font>收藏的圖片如下:</h4>
{% for post in like_post %}
<a href="/one_picture/{{ post.id }}"><img src="{{ static_url(post.thumb_url) }}" width="170" height="170"></a>
{% end %}
</div>
</body>
</html>
3、配置相關(guān)的路由
(r'/profile', main.ProfileHandler),