Django 1.10.3 Tutorial : 관리자 목록 보여주기


  • views.py 를 삭제하고, views 디렉토리를 생성한다.
    $ cd $PRJNAME/web/prjname
    $ rm -rf views.py
    $ mkdir views
    $ cd views
    $ touch __init__.py
    $ mkdir mgmt
    $ cd mgmt
    $ touch __init__.py
  • $PRJNAME/web/prjname/views/mgmt 에 admin.py 를 생성한다.
    # -*- encoding: utf-8 -*-
    # prjname/views/mgmt/admin.py
     
    from django.contrib import messages
    from django.shortcuts import render, redirect
     
    from ...models.admin import *
    # from ...forms.mgmt.admin import *
    # from .helper import login_required
     
     
    # 관리자 목록
    def index(request):
        admins = AdminModel.objects.all()
        return render(request, 'mgmt/admin/index.html', {'menu': 'admin', 'admins': admins})
  • templates 디렉토리를 만들어보자.
    $ cd $PRJNAME/web/prjname
    $ mkdir templates
    $ cd templates
    $ mkdir mgmt
    $ cd mgmt
  • $PRJNAME/web/prjname/templates/mgmt 에 base.html 을 생성한다.
    base.html
    <!DOCTYPE html>
    <html lang="ko">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Management</title>
     
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
     
     
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
     
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </head>
    <body>
    <!--
    <div class="alert alert-success alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    </div>
    <div class="alert alert-warning alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    </div>
    -->
    {% if messages %}
    <div class="alert alert-success alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      {% for message in messages %}
      {{ message }}
      {% endfor %}
    </div>
    {% endif %}
    <nav class="navbar navbar-inverse navbar-static-top">
      <div class="container">
        <div class="navbar-header"> <a class="navbar-brand" href="#">Management</a> </div>
     
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li {% if menu == 'admin' %}class="active"{% endif %}><a href="{% url 'mgmt_admin_index' %}">Admin</a></li>
            <li {% if menu == 'user'  %}class="active"{% endif %}><a href="">User</a></li>
            <li {% if menu == 'notice'  %}class="active"{% endif %}><a href="">Notice</a></li>
            <li {% if menu == 'faq'  %}class="active"{% endif %}><a href="">FAQ</a></li>
            <li {% if menu == 'qna'  %}class="active"{% endif %}><a href="">Q&A</a></li>
          </ul>
     
          <ul class="nav navbar-nav navbar-right">
            <li><a href="">Logout</a></li>
          </ul>
        </div>
      </div>
    </nav>
     
    <div class="container">
    {% block content %}{% endblock %}
    </div>
     
    <script>
    // Modal Remote Reload
    $(document).on('hidden.bs.modal', function (e) {
        $(e.target).removeData('bs.modal');
    });
     
    /*
    $("a[data-target=#updateModal]").click(function(ev) {
        ev.preventDefault();
        var target = $(this).attr("href");
     
        // load the url and show modal on success
        $("#updateModal .modal-body").load(target, function() {
             $("#updateModal").modal("show");
        });
    });
    */
    </script>
    </body>
    </html>
  • admin 디렉토리를 만들고, 그 안에 index.html 파일을 생성한다.
    $ cd $PRJNAME/web/prjname/templates/mgmt
    $ mkdir admin
    index.html
    {% extends "mgmt/base.html" %}
    {% block content %}
    <div class="page-header">
      <h1>Administrator <small>(관리자)</small></h1>
    </div>
     
    <div style="text-align: right">
      <a href="/mgmt/admin/insert_form" class="btn btn-default" data-toggle="modal" data-target="#myModal">관리자 추가</a>
    </div>
     
    <table class="table table-striped table-hover table-condensed" style="margin-top: 20px">
    <tr>
      <th style="text-align: center">아이디</th>
      <th style="text-align: center">별명</th>
      <th style="text-align: center">핸드폰</th>
      <th style="text-align: center">등록일</th>
      <th style="text-align: center">마지막접속시간</th>
      <th style="text-align: center">수정/삭제</th>
    </tr>
    {% for admin in admins %}
    <tr>
      <td style="text-align: center">{{ admin.userid }}</td>
      <td style="text-align: center">{{ admin.nick }}</td>
      <td style="text-align: center">{{ admin.mobile }}</td>
      <td style="text-align: center">{{ admin.reg_date | date:"Y-m-d"}}</td>
      <td style="text-align: center">{% if admin.last_login != None %}{{ admin.last_login | date:"Y-m-d H:i:s" }}{% else %}-{% endif %}</td>
      <td style="text-align: center">
        <a href="/mgmt/admin/password_form/{{ admin.id }}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">비밀번호변경</a>
        <a href="/mgmt/admin/update_form/{{ admin.id }}" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal">수정</a>
        <button onclick="delete_admin('/mgmt/admin/delete/{{ admin.id }}')" class="btn btn-default btn-xs">삭제</button>
      </td>
    </tr>
    {% endfor %}
    </table>
     
    <div id="myModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
        </div>
      </div>
    </div>
     
    <script>
    function delete_admin(url) {
        var result = confirm("관리자를 정말로 삭제하시겠습니까?");
        if( result == false ) return;
        location.href = url;
    }
    </script>
    {% endblock %}
  • $PRJNAME/web/web/urls.py 에 다음과 같이 추가해준다.
    urls.py
    from django.conf.urls import include, url
    # from django.contrib import admin
     
    urlpatterns = [
        # url(r'^admin/', admin.site.urls),
        url(r'', include('prjname.urls')),
    ]
  • $PRJNAME/web/prjname 에 urls.py 를 생성한다.
    urls.py
    from django.conf.urls import url
     
    from .views.mgmt import admin as mgmt_admin
     
    urlpatterns = [
        url(r'^mgmt/admin/$', mgmt_admin.index, name='mgmt_admin_index'),
        # url(r'^mgmt/admin/insert_form$', mgmt_admin.insert_form, name='mgmt_admin_insert_form'),
        # url(r'^mgmt/admin/insert$', mgmt_admin.insert, name='mgmt_admin_insert'),
        # url(r'^mgmt/admin/update_form/(?P<id>[0-9]+)$', mgmt_admin.update_form, name='mgmt_admin_update_form'),
        # url(r'^mgmt/admin/update$', mgmt_admin.update, name='mgmt_admin_update'),
        # url(r'^mgmt/admin/password_form/(?P<id>[0-9]+)$', mgmt_admin.password_form, name='mgmt_admin_password_form'),
        # url(r'^mgmt/admin/change_password$', mgmt_admin.change_password, name='mgmt_admin_change_password'),
        # url(r'^mgmt/admin/delete/(?P<id>[0-9]+)$', mgmt_admin.delete, name='mgmt_admin_delete'),
    ]


댓글

이 블로그의 인기 게시물

야마하 디지털 피아노 YDP-140

테이블위로 마우스 커서 이동시 색깔 변경하기

피아노 연습