Django-公告管理原理和内容

HTML

<table class="table">
                                    <thead>
                                    <tr>
                                        <th style="width: 10%;">序号</th>
                                        <th>标题</th>
                                        <th style="width: 25%;text-align: right;"></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {% for announcement in announcements %}
                                        <tr>
                                            <td>{{ announcement.id }}</td>
                                            <td>{{ announcement.a_title }}</td>
                                            <td>
                                                <a href="/single-an-{{ announcement.id }}">
                                                    <button class="btn" style="float: right;">查看</button>
                                                </a>
                                                <button class="btn delete" style="float: right;"
                                                        onclick="deletea(this);" iddd="{{ announcement.id }}">删除
                                                </button>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
</table>



<form class="form-horizontal form-material">
                                <span>添加公告</span>

                                <div class="form-group" style="margin-top: 10px">
                                    <label class="col-md-12">标题</label>
                                    <div class="col-md-12">
                                        <input id="a_title" type="text" placeholder=""
                                               class="form-control form-control-line">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-12">公告内容</label>
                                    <div class="col-md-12">
                                        <textarea id="a_content" rows="5"
                                                  class="form-control form-control-line"></textarea>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <button id="publisha" class="btn btn-success">发布公告</button>
                                    </div>
                                </div>
</form>
<script>
    function deletea(ths) {
        var aid = $(ths).attr("iddd");
        alert(aid);
        $.ajax({
            url: window.location.href,
            type: "POST",
            data: {'type': 'delete', 'a_id': aid},
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串
                var dic = JSON.parse(data)
                if (!dic.status) alert(JSON.parse(data).msg);
                else window.location.href = "/announcement";
            }
        });
    }

    $('#publisha').click(function () {
        var a_title = $('#a_title').val();
        var a_content = $('#a_content').val();
        $.ajax({
            url: window.location.href,
            type: "POST",
            data: {'type': 'create', 'a_content': a_content, 'a_title': a_title},
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串
                var dic = JSON.parse(data);
                if (!dic.status) alert(JSON.parse(data).msg);
                else window.location.href = "/announcement";
            }
        });
    });
</script>

views.py

# 公告管理
def announcement(request):
    if not request.session.get('admin_uid'):
        return redirect('/my-admin')

    # 查询所有公告
    if request.method == 'GET':

        announcements = models.Announcement.objects.filter()
        response = {'announcements': announcements}
        return render(request, 'announcement.html', response)

    # 发公告,删公告
    elif request.method == 'POST':
        p_type = request.POST.get('type')
        response = {'msg': '', 'status': False}
        if p_type == 'delete':
            a_id = request.POST.get('a_id')
            models.Announcement.objects.filter(id=a_id).delete()
            response['status'] = True
        elif p_type == 'create':
            # 添加一条公告
            a_title = request.POST.get('a_title')
            a_content = request.POST.get('a_content')
            models.Announcement.objects.create(a_title=a_title, a_content=a_content)
            response['status'] = True
        return HttpResponse(json.dumps(response))

models.py

# 公告表
class Announcement(models.Model):
    # a_id = models.CharField(verbose_name='公告id', max_length=16)
    a_title = models.CharField(verbose_name='公告标题', max_length=64)
    a_content = models.CharField(verbose_name='公告内容', max_length=3000, null=True)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0wioiw0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值