def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } # username = current_user(request) # if username == '游客': # # 没登录 不让看 重定向到 / # return redirect('/login') # else: header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) def weibo_tag(weibo): return '<p>{} from {}@{} <a href="/weibo/delete?id={}">删除</a></p>'.format( weibo.content, user.username, weibo.created_time, weibo.id, ) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user.id) # 任一 user 访问任一 index current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) # format 函数的字典用法 # 注意 u.id 是 current_user # user.username 是博主 w = { 'id': weibo.id, 'user_id': u.id, 'content': weibo.content, 'username': user.username, 'time': weibo.created_time, 'comments': comments, } # 手动处理 weibos 这个 list # 把每个 weibo 以 <p> 的形式展现在页面 return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a></p> <button class="weibo-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="weibo-comment-form weibo-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) # 用 join() 返回 str weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def index(request): user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return redirect('/login') # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) body = template('weibo_index.html', weibos=weibos, user=user) return http_response(body)
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) w = { "id": weibo.id, "user_id": u.id, "content": weibo.content, "username": user.username, "time": weibo.created_time, "comments": comments, } log('comments debug', comment_list) return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a> <button class="gua-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="gua-comment-form gua-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def detail(request): u = current_user(request) weibo_list = Weibo.find_all(user_id=u.id) body = template('weibo_detail.html', weibos=weibo_list) return http_response(body)