2015-10-22

beego Tutorial 4 : 관리자 목록 보여주기



  • xyz/models/admin.go 에 다음을 추가한다.
    import (
        "github.com/astaxie/beego/orm"
    )
     
    ...
     
    func (this *Admin) GetAll() []*Admin {
        var admins []*Admin
     
        o := orm.NewOrm()
        o.Using("default")
        o.QueryTable(this).All(&admins)
     
        return admins
    }
     
    ...
  • xyz/controllers/admin.go 파일을 생성한다.
    package controllers
     
    import (
        "github.com/astaxie/beego"
        "xyz/models"
    )
     
    type AdminController struct {
        beego.Controller
    }
     
    // 관리자 목록
    func (c *AdminController) Index() {
        admin := new(models.Admin)
        admins := admin.GetAll()
     
        c.Data["Admins"] = admins
        c.TplNames = "admin/index.html"
    }
  • xyz/views/admin/index.html 파일을 생성한다.
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>관리자 목록</title>
    </head>
    <body>
    <h1>Administrator (관리자)</h1>
     
    <table>
    <tr>
      <th style="text-align: center">아이디</th>
      <th style="text-align: center">별명</th>
    </tr>
    {{range $admins := .Admins}}
    <tr>
      <td style="text-align: center">{{$admins.Userid}}</td>
      <td style="text-align: center">{{$admins.Nick}}</td>
    </tr>
    {{end}}
    </table>
    </body>
    </html>
  • xyz/routers/router.go 에 다음을 추가한다.
        beego.Router("/admin", &c.AdminController{}, "get:Index")
  • 화면 디자인이 별로이니, Bootstrap 을 적용해보자. http://getbootstrap.com/ 에서 최신버전을 다운로드 하자.
  • 다운로드한 부트스트랩을 xyz/static/b 디렉토리에 풀어서 넣자.
    ├── static
    │   ├── b
    │   │   ├── css
    │   │   │   ├── bootstrap.css
    │   │   │   ├── bootstrap.css.map
    │   │   │   ├── bootstrap.min.css
    │   │   │   ├── bootstrap-theme.css
    │   │   │   ├── bootstrap-theme.css.map
    │   │   │   └── bootstrap-theme.min.css
    │   │   ├── fonts
    │   │   │   ├── glyphicons-halflings-regular.eot
    │   │   │   ├── glyphicons-halflings-regular.svg
    │   │   │   ├── glyphicons-halflings-regular.ttf
    │   │   │   ├── glyphicons-halflings-regular.woff
    │   │   │   └── glyphicons-halflings-regular.woff2
    │   │   └── js
    │   │       ├── bootstrap.js
    │   │       ├── bootstrap.min.js
    │   │       └── npm.js
    │   ├── css
    │   ├── img
    │   └── js
    
  • xyz/views/admin/index.html 파일을 다음과 같이 수정한다.
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>관리자 목록</title>
    <link href="/static/b/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="/static/b/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container">
      <div class="page-header">
        <h1>Administrator <small>(관리자)</small></h1>
      </div>
     
      <table class="table table-striped table-hover table-condensed">
      <tr>
        <th style="text-align: center">아이디</th>
        <th style="text-align: center">별명</th>
      </tr>
      {{range $admins := .Admins}}
      <tr>
        <td style="text-align: center">{{$admins.Userid}}</td>
        <td style="text-align: center">{{$admins.Nick}}</td>
      </tr>
      {{end}}
      </table>
    </div>
    </body>
    </html>

댓글 없음:

댓글 쓰기