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)