예제 #1
0
def add_article(request):
    if request.method == "POST" and request.is_ajax():
        # 获取post提交的数据
        category_name = request.POST.get("category")
        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)
        category = Category.objects.get(name=category_name)
        add = request.POST.get("add")
        if add == "false":  # 编辑文章
            pass
        else:
            print request.POST.get("id")
            category.count = category.count + 1
            category.save()
        article = Article(user=user, category=category, title=title, content=content, author=author)
        article.id = request.POST.get("id")  # 如果是编辑文章将会获取到原文章的id, 如果是添加文章则获取到none
        article.save()
        category_list = Category.objects.filter(user=request.user)
        content_dict = {"categories": category_list}
        return HttpResponse("添加成功")
    else:
        category_list = Category.objects.filter(user=request.user)
        userprofile = UserProfile.objects.get(id=request.user.id)
        content_dict = {"categories": category_list, "userprofile": userprofile}
        return render(request, "admin/add_article.htm", content_dict)