python django框架 渲染和ajax表單提交周報(bào)表

一. 實(shí)際需求

  1. 之前這篇文章kettle-自動(dòng)生成周報(bào) 是使用java開(kāi)源ETL工具kettle實(shí)現(xiàn),正常情況下是每天寫(xiě)工作完成情況到Excel表,每周五定時(shí)任務(wù)啟動(dòng)讀取Excel日志寫(xiě)入數(shù)據(jù)庫(kù),再讀取數(shù)據(jù)庫(kù)內(nèi)容按照公司周報(bào)模板生成文件,發(fā)郵件,上傳到SVN,如果有天遺忘寫(xiě)工作進(jìn)度,需要數(shù)據(jù)庫(kù)插入一條記錄.
  2. 當(dāng)通過(guò)web頁(yè)面提交數(shù)據(jù),需要做到的功能有哪些?
    需要考慮到實(shí)現(xiàn)插入新記錄,刪除記錄(測(cè)試時(shí)候?yàn)闉榱俗屩麈Iid從1開(kāi)始 刪除默認(rèn)全部刪除),更新某條記錄的值,導(dǎo)出excel文件,也就是基本上的增刪改查導(dǎo)出操作

二.項(xiàng)目操作

2.1 diango項(xiàng)目新增一個(gè)app python manage.py startapp weekreport,其次數(shù)據(jù)庫(kù)建一個(gè)周報(bào)日志表,插入幾條數(shù)據(jù).

CREATE TABLE `weekreport` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
  `content` text COMMENT '周報(bào)內(nèi)容',
  `create_time` varchar(50) DEFAULT NULL COMMENT '完成時(shí)間',
  `finished_status` varchar(50) DEFAULT NULL COMMENT '完成情況',
  `cooperation` varchar(50) DEFAULT NULL COMMENT '協(xié)同部門(mén)/人',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.2 其次讀取這個(gè)表的數(shù)據(jù) 渲染到頁(yè)面(依舊沒(méi)用ORM模型).
有些細(xì)節(jié) 比如 新增一行 按鈕點(diǎn)擊以后,需要根據(jù)數(shù)據(jù)庫(kù)總記錄數(shù)+1生成一個(gè)id,完成時(shí)間點(diǎn)擊可以彈出當(dāng)日時(shí)間,也可以手動(dòng)修改時(shí)間.總而言之,數(shù)據(jù)表改變后都需要重載當(dāng)前表格,導(dǎo)出文件需要導(dǎo)出到指定文件夾,生成的文件和原來(lái)kettle生成的一樣.


web頁(yè)面

除了新增一行沒(méi)有調(diào)用接口,純js生成新的一行記錄,其余4個(gè)按鈕點(diǎn)擊時(shí)候通過(guò)jQuery的ajax請(qǐng)求后臺(tái)接口.
2.3 前端頁(yè)面(原諒我low的前端....)

<!DOCTYPE html>
<html>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>周報(bào)</title>
    <link rel="stylesheet" >
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/static/css/templatemo_main.css">
    <style>
        table {
            border-spacing: 0;
            border-collapse: collapse
        }
        th {
            background-color: #79aec8;
            border: 1px #609ab6 solid;
        }
    </style>
</head>
<body>
<!-- <table class="table table-bordered table-striped table-hover table-condensed"-->
<div align="center" style="width: 100%;font-size:15px">
    <h3>周報(bào)</h3>
    <div class="container" align="center">
        <div class="row">
            <div class="col-md-12">
                <table id="oTable" class="table table-hover table-bordered table-condensed table-striped">
                    <thead>
                    <tr class="info">
                        <th>序號(hào)</th>
                        <th>工作內(nèi)容</th>
                        <th>完成時(shí)間</th>
                        <th>完成情況</th>
                        <th>協(xié)同部門(mén)/人</th>
                    </tr>
                    </thead>
                    <tbody id="tr_tbody">
                    {% for data in week_reports_data %}
                        <tr class="active">
                            <td class="col-md-2"><input style='width:100%' value="{{ data.id }}" id='id1' name="id1">
                            </td>
                            <td class="col-md-4"><input style='width:100%' value="{{ data.content }}" id='content1'
                                                        name="content1"></td>
                            <td class="col-md-2"><input style='width:100%' value="{{ data.create_time }}"
                                                        id='create_time1' name="create_time1"></td>
                            <td class="col-md-2"><input style='width:100%' value="{{ data.finished_status }}"
                                                        id='finished_status1' name="finished_status1"></td>
                            <td class="col-md-2"><input style='width:100%' value="{{ data.cooperation }}"
                                                        id='cooperation1' name="cooperation1"></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
                <input type="button" value="新增一行" id="btn1" onclick="Addrow()">
                <button type="button" id="commit">提交保存</button>
                <button type="button" id="update">更新周報(bào)</button>
                <button type="button" id="delete">刪除全部</button>
                <button id="export_excel" type="button">導(dǎo)出excel</button>
                <script type="text/javascript">
                    function Addrow() {
                        var id = "<tr><td class='col-md-2'><input style='width:100%' type='text'  id='id' name='id' value={{ max_id }}></td>";
                        var content = "<td class='col-md-4'><input style='width:100%' type='text'  id='content' name='content' value=''></td>";
                        var create_time = "<td class='col-md-2'><input style='width:100%' type='text'  id='create_time' name='create_time' onclick='get_now_time()'></td>";
                        var finished_status = "<td class='col-md-2'><input style='width:100%' type='text' id= 'finished_status' name='finished_status' value='' ></td>";
                        var cooperation = "<td class='col-md-2'><input style='width:100%' type='text'  id='cooperation' name='cooperation' value='' ></td></tr>";
                        var str_html = id + content + create_time + finished_status + cooperation;
                        $("#oTable").append(str_html);
                    }
                </script>
                <script type="text/javascript">
                    function get_now_time() {
                        function p(s) {
                            return s < 10 ? '0' + s : s;
                        }
                        var myDate = new Date();
                        var year = myDate.getFullYear();
                        var month = myDate.getMonth() + 1;
                        var date = myDate.getDate();
                        var now_time = year + '-' + p(month) + "-" + p(date);
                        var x = document.getElementById('create_time');
                        x.value = now_time;
                    }
                </script>
                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#commit").click(function () {
                            $.ajax({
                                type: 'POST',
                                url: "{% url 'weekreport:add_report' %} ",
                                data: {
                                    id: $('#id').val(),
                                    content: $('#content').val(),
                                    create_time: $('#create_time').val(),
                                    finished_status: $('#finished_status').val(),
                                    cooperation: $('#cooperation').val(),
                                    csrfmiddlewaretoken: '{{ csrf_token }}'
                                },
                                success: function (data) {
                                    alert(data);
                                    window.location.reload();
                                }
                            });
                        });
                    });
                </script>
                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#update").click(function () {
                            $.ajax({
                                type: 'POST',
                                url: "{% url 'weekreport:update_report' %} ",
                                data: {
                                    id: $('#id1').val(),
                                    content: $('#content1').val(),
                                    create_time: $('#create_time1').val(),
                                    finished_status: $('#finished_status1').val(),
                                    cooperation: $('#cooperation1').val(),
                                    csrfmiddlewaretoken: '{{ csrf_token }}'
                                },
                                success: function (data) {
                                    alert(data);
                                    window.location.reload();
                                }
                            });
                        });
                    });
                </script>
                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#delete").click(function () {
                            $.ajax({
                                type: 'get',
                                url: "{% url 'weekreport:delete_report' %} ",
                                success: function (data) {
                                    alert(data);
                                    window.location.reload();
                                }
                            });
                        });
                    });
                </script>
                <script type="text/javascript">
                    $(document).ready(function () {
                        $("#export_excel").click(function () {
                            htmlobj = $.ajax({url: "{% url 'weekreport:export_all_excel' %}", async: false});
                            alert(htmlobj.responseText);
                        });
                    });
                </script>
            </div>
        </div>
    </div>
</div>

</body>
</html>

三.實(shí)現(xiàn)

數(shù)據(jù)庫(kù)
導(dǎo)出excel文件
excel內(nèi)容
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,535評(píng)論 6 13
  • 一、溫故而知新 1. 內(nèi)存不夠怎么辦 內(nèi)存簡(jiǎn)單分配策略的問(wèn)題地址空間不隔離內(nèi)存使用效率低程序運(yùn)行的地址不確定 關(guān)于...
    SeanCST閱讀 8,141評(píng)論 0 27
  • 1.首先上支付寶移動(dòng)支付官方文檔地址 網(wǎng)頁(yè)如下圖: 下載SDK,并按照集成詳細(xì)流程里面需要導(dǎo)入的依賴(lài)圖導(dǎo)入相關(guān)依賴(lài)...
    antns丶閱讀 1,028評(píng)論 0 3
  • 認(rèn)識(shí)軒哥快七年,在我最美好的年紀(jì)相遇。曾以為告別初戀,人生不會(huì)再有這樣的甜言蜜語(yǔ),不會(huì)再為某個(gè)男生深夜寫(xiě)情詩(shī),不會(huì)...
    孫潤(rùn)閱讀 505評(píng)論 2 2

友情鏈接更多精彩內(nèi)容