def add_ownmessage_view(request): """ 添加主人寄语 """ tp = "manager/create_ownmessage.html" context = { "active_classes": ['.blog', '.ownmessage_list'], } if request.method == "GET": return render(request, tp, context) if request.method == "POST": form = OperateOwnMessageForm(request.POST) if not form.is_valid(): messages.warning(request, "</br>".join(form_error(form))) return HttpResponseRedirect(reverse('ownmessage_list')) try: OwnerMessage.objects.create( summary=form.cleaned_data.get("summary"), message=form.cleaned_data.get("message"), editor=EditorKind.Markdown) messages.success(request, '添加成功') return HttpResponseRedirect(reverse('ownmessage_list')) except Exception as ex: messages.warning(request, ex) return HttpResponseRedirect(reverse('ownmessage_list'))
def edit_ownmessage_view(request, item_id): """ 主人寄语编辑 :param request: :return: """ message = OwnerMessage.objects.filter(id=item_id).first() if not message: messages.warning(request, "此主人寄语不存在") return HttpResponseRedirect(reverse('ownmessage_list')) tp = "manager/edit_ownmessage.html" context = { "active_classes": ['.blog', '.ownmessage_list'], "message": message, "item_id": item_id } if request.method == "GET": return render(request, tp, context) if request.method == "POST": form = OperateOwnMessageForm(request.POST) if not form.is_valid(): messages.warning(request, "</br>".join(form_error(form))) return HttpResponseRedirect(reverse('ownmessage_list')) data = { "summary": form.cleaned_data.get("summary"), "message": form.cleaned_data.get("message"), "editor": form.cleaned_data.get("editor"), "last_update": datetime.now() } try: OwnerMessage.objects.filter(id=item_id).update(**data) messages.success(request, '修改成功') return HttpResponseRedirect(reverse('ownmessage_list')) except Exception as ex: messages.warning(request, ex) return HttpResponseRedirect(reverse('ownmessage_list'))