Python快速建站系列-Part.Five.3-个人主页及资料页面

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

第五部分最后一节,完成个人主页里资料页面的个人资料的展示和修改功能,不过毕竟功能比较少,个人资料其实只有昵称一项,手动滑稽。


一如既往先写出来userinfo.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="/code/list">算法</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="/user/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="/user/usercen">个人中心</a>
                                    </li>
                                    <li>
                                        <a href="/code/codelist">我的代码</a>
                                    </li>
                                    <li>
                                        <a href="/user/plan">我的计划</a>
                                    </li>
                                    <li class="divider">
                                    </li>
                                    <li>
                                        <a href="/sign/out">退出登录</a>
                                    </li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </nav>
                <ul class="nav nav-tabs">
                <li>
                     <a href="/user/usercen">写文章</a>
                </li>
                <li>
                     <a href="/user/userblog">个人文章目录</a>
                </li>
                <li class="active">
                     <a href="#">个人资料</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="#">修改密码</a>
                        </li>
                    </ul>
                </li>
            </ul>
            <br><br><br><br><br><br>
            <blockquote>
                <p>
                    {{ userinfo['username'] }}
                </p> <small>This is your<cite>用户名</cite></small>
            </blockquote>
            <blockquote>
                <p>
                    {{ userinfo['nickname'] }}
                </p> <small>This is your <cite>昵称</cite></small>
            </blockquote>
            <form method="POST">
                修改昵称
                <input type="text" name="newname" value="">
                <button type="submit">提交</button>
            </form>
{% end %}

接着在UserHandler中完善进userinfo的部分↓

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('/')
        if input == 'userblog':
            coll = self.application.db.blogs
            blogs = coll.find({'author':self.get_cookie('nickname')}).sort('datetime',pymongo.DESCENDING)
            self.render('user/userblog.html',username=self.get_cookie('nickname'),blogs = blogs)
        if input == 'userinfo':
            coll = self.application.db.users
            userinfo = coll.find_one({'username':self.get_cookie('username')})
            self.render('userinfo.html',username=userinfo['username'],userinfo=userinfo)
    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')
        if input == 'userinfo':
            nickname = self.get_argument('newname')
            coll = self.application.db.users
            try:
                findone = coll.find_one({'username':self.get_cookie('username')})
                findone['nickname']=nickname
                coll.save(findone)
                self.redirect('/userinfo')
            except:
                self.write({'error':'edit the nickname wrongly'})

个人主页里的资料页面完成。

--Part.Five.3--

--Part.Five--

上一篇:JavaScript document属性和方法


下一篇:[每天默写一个算法]KMP