def add_essays(request): if request.method == "POST" and request.is_ajax(): # 获取post提交的数据 eid = "" title = request.POST.get("title") author = request.POST.get("author") content = request.POST.get("content") # print 'category:' + category_name + ' title:' + title + ' author:' + author + ' content:' + content #打印POST请求的内容 # 创建Article对象,保存文章 user = User.objects.get(username=request.user.username) add = request.POST.get("add") if add == "false": # 编辑文章 eid = request.POST.get("id") essays = Essays.objects.get(id=eid) essays.delete() print "delete essays success" else: pass essays = Essays(user=user, title=title, content=content, author=author) if eid != "": essays.id = eid essays.save() return HttpResponse("添加成功") else: userprofile = UserProfile.objects.get(id=request.user.id) content_dict = {"userprofile": userprofile} return render(request, "admin/add_essays.htm", content_dict)
def add_essays(user, title, author, content): essays = Essays(user=user, title=title, author=author, content=content) essays.save() return essays