python---tornado初識(2)實現登陸和發佈文章

# coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python

import tornado.ioloop import tornado.web import uimethod as mt import uimodule as md name_list=[] #用戶信息
USER_INFO={'login_status':None,'name':None} #文章列表
NEWS_LIST=[ {"title":"dfwafaw","content":"這都是字母"} ] #原來測試模板
class MainHandler(tornado.web.RequestHandler): def get(self): # self.write("Hello World")
        # 默認當前路徑尋找
        # print(self.get_argument('name'))
        # print(self.get_argument('age'))
        self.render("s1.html",ccc=name_list,nm="mmp") def post(self, *args, **kwargs): name=self.get_argument('xxx') name_list.append(name) self.render("s1.html",ccc=name_list) #顯示首頁
class HomeHandler(tornado.web.RequestHandler): def get(self): self.render('index.html',user_status=USER_INFO['login_status'],user_name=USER_INFO['name'],content=NEWS_LIST) #傳參也能夠是字典,在模板按字典使用

#處理登陸和退出
class LoginHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): name=self.get_argument('username',None) pawd=self.get_argument('password',None)#沒法獲取時候設爲None,防止出錯
        if name == "ld" and pawd == '123456': USER_INFO['login_status']=True USER_INFO['name']=name self.redirect('/home') def get(self, *args, **kwargs): status=self.get_argument('quit') if status == 'true': USER_INFO['login_status']=False USER_INFO['name']=None self.redirect('/home') #文章發佈
class AddNewsHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): title=self.get_argument('title',None) content=self.get_argument('content',None) if title and USER_INFO['login_status']: NEWS_LIST.append({"title":title,"content":content}) #作一個跳轉
        self.redirect('home')#是跳轉方法,不是頁面
 st ={ "template_path": "template",#模板路徑配置
    "static_path":'static',    #js css等靜態文件路徑配置 不管這裏配置了什麼路徑,在靜態文件中使用都是用static
    "static_url_path":'/ss/', #在static_path必須存在的基礎上 相似於對其取了一個別名
    #如果沒有static_url_prefix,則在靜態文件中的資源獲取爲static/s1.css
    #當存在static_url_prefix時,(前提已經存在static_path),這時具體路徑程序已經獲取,你只須要在資源前面加上這個前綴,不須要本身去寫具體url
    #就是能夠看作爲static_path起了一個別名
    #static_url_prefix瞭解便可,不經常使用
    'ui_methods':mt, #自定義函數在模板中使用{{}}
    'ui_modules':md, #自定義類在模板中使用{% %}
} #路由映射 匹配執行,不然404
application = tornado.web.Application([ (r"/index",MainHandler), (r"/home",HomeHandler), (r"/login",LoginHandler), (r"/addNews",AddNewsHandler), ],**st) if __name__=="__main__": application.listen(8080) #io多路複用
    tornado.ioloop.IOLoop.instance().start()

模板文件css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<link rel="stylesheet" href="static/plugins/bootstrap3/css/bootstrap.css">-->
    <!--<script src="static/js/jquery.js"></script>-->
    <!--<script src="static/plugins/bootstrap3/js/bootstrap.js"></script>-->

    <!--注意要想在模板文件中使用static_url必須定義static_url_prex或者static_url_path,二者相似-->
    <link rel="stylesheet" href="{{static_url('plugins/bootstrap3/css/bootstrap.css')}}}">
    <link rel="stylesheet" href="{{static_url('css/index.css')}}">
    <script src='{{static_url("js/jquery.js")}}'></script >
    <script src="{{static_url('plugins/bootstrap3/js/bootstrap.js')}}"></script>

</head>
<body>
    <div> {% if user_status %} <h1>你好:{{user_name}}<a onclick="PostNews();">發佈消息</a><div style="float: right;"><a href="/login?quit=true">退出</a></div></h1> {% else %} <h1>請先<a onclick="Login();">登陸</a></h1> {% end %} </div>
    <div class="content-list"> {% for item in content %} <div class="item">
            <div class="title">{{item['title']}}</div>
            <div class="content">{{item['content']}}</div>
        </div> {% end %} </div>
    <div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <form action="login" method="post">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">用戶登陸</h4>
          </div>
          <div class="modal-body">
              <div class="form-group">
                <label class="control-label">用戶名:</label>
                <input type="text" class="form-control" name="username">
              </div>
              <div class="form-group">
                <label class="control-label">密碼:</label>
                <input type="password" class="form-control" name="password">
              </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
          </form>
        </div>
      </div>
    </div>

    <div class="modal fade" id="pub" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <form action="addNews" method="post">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">用戶登陸</h4>
          </div>
          <div class="modal-body">
              <div class="form-group">
                <label class="control-label">標題:</label>
                <input type="text" class="form-control" name="title">
              </div>
              <div class="form-group">
                <label class="control-label">內容:</label>
                <textarea class="form-control" name="content" id="message-text"></textarea>
              </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
          </form>
        </div>
      </div>
    </div>
</body>
</html>
<script>
    function Login(){ $("#login").modal('show'); } function PostNews(){ $("#pub").modal('show'); } </script>
相關文章
相關標籤/搜索