|版权声明:本文为博主原创文章,未经博主允许不得转载。
Part.Three中实现了注册和登录的功能,那这一节完成主页内容的填充,并且主页中要实现简单的可以查找代码的功能。
而且有于公共代码部分存储在本机,所以用存储路径的方式存储本地代码的信息。
先写了index.html↓
1 {% extends "main.html" %} 2 {% block content %} 3 <nav class="navbar navbar-default navbar-static-top" role="navigation"> 4 <div class="navbar-header"> 5 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="#">TSSS</a> 6 </div> 7 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> 8 <ul class="nav navbar-nav"> 9 <li> 10 <a href="/">首页</a> 11 </li> 12 <li> 13 <a href="/code/list">算法</a> 14 </li> 15 <li class="dropdown"> 16 <a href="#" class="dropdown-toggle" data-toggle="dropdown">代码<strong class="caret"></strong></a> 17 <ul class="dropdown-menu"> 18 <li> 19 <a href="https://github.com/MU001999/The-algorithms">GitHub</a> 20 </li> 21 <li> 22 <a href="https://leetcode.com/">LeetCode</a> 23 </li> 24 <li> 25 <a href="https://www.codewars.com/">CodeWars</a> 26 </li> 27 <li class="divider"> 28 </li> 29 <li> 30 <a href="https://github.com/sxy1001">SXY1001</a> 31 </li> 32 <li class="divider"> 33 </li> 34 <li> 35 <a href="https://github.com/MU001999">MU001999</a> 36 </li> 37 </ul> 38 </li> 39 </ul> 40 <form class="navbar-form navbar-left" role="search" action="/" method="POST"> 41 <div class="form-group"> 42 <input type="text" class="form-control" name="codename" /> 43 </div> <button type="submit" class="btn btn-default">查找</button> 44 </form> 45 {%if not cookieName%} 46 <ul class="nav navbar-nav navbar-right"> 47 <li> 48 <a href="/sign/in">登入</a> 49 </li> 50 <li> 51 <a href="/sign/up">注册</a> 52 </li> 53 </ul> 54 {% else %} 55 <ul class="nav navbar-nav navbar-right"> 56 <li> 57 <a href="/user/main">{{ username }}</a> 58 </li> 59 <li class="dropdown"> 60 <a href="#" class="dropdown-toggle" data-toggle="dropdown">更多<strong class="caret"></strong></a> 61 <ul class="dropdown-menu"> 62 <li> 63 <a href="/user/main">个人中心</a> 64 </li> 65 <li> 66 <a href="/user/code">我的代码</a> 67 </li> 68 <li> 69 <a href="/user/plan">我的计划</a> 70 </li> 71 <li class="divider"> 72 </li> 73 <li> 74 <a href="/sign/out">退出登录</a> 75 </li> 76 </ul> 77 </li> 78 </ul> 79 {% end %} 80 </div> 81 </nav> 82 <span class="label label-danger">本日最佳</span> 83 <div class="jumbotron well"> 84 <h1> 85 Hello, world! 86 </h1> 87 <p> 88 #include <stdio.h> 89 </p> 90 <p> 91 int main() 92 </p> 93 <p> 94 { 95 </p> 96 <p> 97 printf("Hello,World!"); 98 </p> 99 <p> 100 return 0; 101 </p> 102 <p> 103 } 104 </p> 105 <p> 106 <a class="btn btn-primary btn-large" href="#">查看讨论</a> 107 </p> 108 </div> 109 <div class="list-group"> 110 <a href="#" class="list-group-item active">最新文章</a> {% for blog in blogs %} 111 <div class="list-group-item"> 112 <a href="/blog/{{ blog[‘_id‘] }}">{{ blog[‘title‘] }}</a> 113 </div> 114 {% end %} 115 </div> 116 {% end %}
↑其中Hello world那部分可以以后根据评论数排序出来的第一个换掉。
再来看一下数据库规范↓
codedb:blogs _id ID author 作者 title 题目 content 内容 datetime 时间
然后写IndexHandler↓
1 class IndexHandler(web.RequestHandler): 2 def get(self): 3 coll = self.application.db.blogs 4 username = self.get_cookie(‘username‘) 5 blogs = coll.find().sort(‘datetime‘,pymongo.DESCENDING) 6 self.(‘index.html‘,username=username,blogs=blogs) 7 def post(self): 8 coll = self.application.db.codes 9 codename = self.get_argument(‘codename‘) 10 codeone = coll.find_one({‘title‘:codename}) 11 if codeone: 12 try: 13 with open(os.path.dirname(__file__),findcode[‘path‘],‘r‘) as f: 14 content = f.read() 15 self.render(‘codeview.html‘,codeone = codeone) 16 except: 17 self.set_status(404) 18 self.write({‘error‘:‘code not be here now‘})
这样主页的内容就填充完成了。
时间: 2024-10-14 02:09:27