Python快速建站系列-Part.Five.1-个人主页及发表文章

时间:2022-05-15 18:01:21

|版权声明:本文为博主原创文章,未经博主允许不得转载。

现在的TSSS已经有了注册和登录的功能,首页的内容也填充好了,那这一节就完成用户个人主页的内容和发表文章功能的实现。


先完成用户个人主页的usercen.html↓

 {% extends "main.html" %}

 {% block content %}
 <nav class="navbar navbar-default navbar-static-top" role="navigation">
                 <div class="navbar-header">
                      <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>
                 </div>
                 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                     <ul class="nav navbar-nav">
                         <li>
                              <a href="/">首页</a>
                         </li>
                         <li>
                              <a href="/codelist">算法</a>
                         </li>
                         <li class="dropdown">
                              <a href="#" class="dropdown-toggle" data-toggle="dropdown">代码<strong class="caret"></strong></a>
                             <ul class="dropdown-menu">
                                 <li>
                                      <a href="https://github.com/MU001999/The-algorithms">GitHub</a>
                                 </li>
                                 <li>
                                      <a href="https://leetcode.com/">LeetCode</a>
                                 </li>
                                 <li>
                                      <a href="https://www.codewars.com/">CodeWars</a>
                                 </li>
                                 <li class="divider">
                                 </li>
                                 <li>
                                      <a href="https://github.com/sxy1001">SXY1001</a>
                                 </li>
                                 <li class="divider">
                                 </li>
                                 <li>
                                      <a href="https://github.com/MU001999">MU001999</a>
                                 </li>
                             </ul>
                         </li>
                     </ul>
                     <form class="navbar-form navbar-left" role="search" action="/" method="POST">
                         <div class="form-group">
                             <input type="text" class="form-control" name="codename" />
                         </div> <button type="submit" class="btn btn-default">查找</button>
                     </form>
                     <ul class="nav navbar-nav navbar-right">
                         <li>
                              <a href="/usercen">{{ username }}</a>
                         </li>
                         <li class="dropdown">
                              <a href="#" class="dropdown-toggle" data-toggle="dropdown">更多<strong class="caret"></strong></a>
                             <ul class="dropdown-menu">
                                 <li>
                                      <a href="/usercen">个人中心</a>
                                 </li>
                                 <li>
                                      <a href="/mycode">我的代码</a>
                                 </li>
                                 <li>
                                      <a href="/myplan/plans">我的计划</a>
                                 </li>
                                 <li class="divider">
                                 </li>
                                 <li>
                                      <a href="/sign-out">退出登录</a>
                                 </li>
                             </ul>
                         </li>
                     </ul>
                 </div>
             </nav>
             <ul class="nav nav-tabs">
                 <li class="active">
                      <a href="#">写文章</a>
                 </li>
                 <li>
                      <a href="/userblog">个人文章目录</a>
                 </li>
                 <li>
                      <a href="/userinfo">个人资料</a>
                 </li>
                 <li class="dropdown pull-right">
                      <a href="#" data-toggle="dropdown" class="dropdown-toggle">↓↓↓<strong class="caret"></strong></a>
                     <ul class="dropdown-menu">
                         <li>
                              <a href="/chapwd">修改密码</a>
                         </li>
                     </ul>
                 </li>
             </ul>
             <br>
             <form action="/usercen" method="POST">
                 <p>题目</p>
                 <input class="form-control" type="text" name="title" value="">
                 <p>内容</p>
                 <textarea rows="20" cols="30" name="content"></textarea>
                 <button type="submit">发布</button>
             </form>
 {% end %}

↑这样写的目的就是为了个人主页直接是写文章的地方。

先再来看一下数据库规范↓

codedb:blogs
    _id                 ID
    author             作者
    title                题目
    content            内容
    datetime             时间

接着完成UserHandler↓

 class UserHandler(web.RequestHandler):
     def get(self,input):
         if input == 'usercen':
             coll = self.application.db.users
             if self.get_cookie('username'):
                 self.render('user/usercen.html',username=self.get_cookie('nickname'))
             else:
                 self.redirect('/')
     def post(self,input):
         if input == 'usercen':
             coll = self.application.db.blogs
             author = self.application.db.users.find_one({'username':self.get_cookie('username')})['nickname']
             title = self.get_argument('title')
             content = self.get_argument('content')
             datetime = time.ctime()
             try:
                 coll.insert({'title':title,'author':author,'content':content,'datetime':datetime})
             finally:
                 self.redirect('/user/userblog')

个人主页里的发表文章功能完成。

--Part.Five.1--