def post(self, request): form = AnnoForm(request.POST) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="添加成功") return HttpResponseRedirect(reverse("sspanel:admin_announcements")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form} return render(request, "my_admin/announcement_create.html", context=context)
def post(self, request, pk): anno = Announcement.objects.get(pk=pk) form = AnnoForm(request.POST, instance=anno) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="修改成功") return HttpResponseRedirect(reverse("sspanel:admin_announcements")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form, "anno": anno} return render(request, "my_admin/announcement_detail.html", context=context)
def anno_edit(request, pk): """公告编辑""" anno = Announcement.objects.get(pk=pk) # 当为post请求时,修改数据 if request.method == "POST": form = AnnoForm(request.POST, instance=anno) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="修改成功") return HttpResponseRedirect(reverse("sspanel:backend_anno")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form, "anno": anno} return render(request, "backend/annoedit.html", context=context) # 当请求不是post时,渲染form else: anno.body = tomd.convert(anno.body) context = {"anno": anno} return render(request, "backend/annoedit.html", context=context)
def anno_create(request): """公告创建""" if request.method == "POST": form = AnnoForm(request.POST) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="添加成功") return HttpResponseRedirect(reverse("sspanel:backend_anno")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form} return render(request, "backend/annocreate.html", context=context) else: form = AnnoForm() return render(request, "backend/annocreate.html", context={"form": form})
def get(self, request): form = AnnoForm() return render(request, "my_admin/announcement_create.html", context={"form": form})