Пример #1
0
def article_create(request):
    # 判断用户是否提交数据
    if request.method == 'POST':
        article_post_form = ArticlePostForm(request.POST, request.FILES)
        if article_post_form.is_valid():
            # 保存数据,但暂时不提交到数据库中
            new_article = article_post_form.save(commit=False)
            # 当前登录用户id
            new_article.author = User.objects.get(id=request.user.id)
            # 新增的代码
            if request.POST['column'] != 'none':
                new_article.column = ArticleColumn.objects.get(
                    id=request.POST['column'])
            # 保存article
            new_article.save()
            # 保存 tags 的多对多关系
            article_post_form.save_m2m()
            return redirect('article:article-list')
        else:
            return HttpResponse("表单内容有误,请重新填写。")
    else:
        # 创建表单类实例
        article_post_form = ArticlePostForm()
        # 新增及修改的代码
        columns = ArticleColumn.objects.all()
        context = {'article_post_form': article_post_form, 'columns': columns}
        # 返回模板
        return render(request, 'article/create.html', context)
Пример #2
0
def article_create(request):
    # 判断用户是否提交数据
    if request.method == 'POST':
        # 将提交的数据赋值到表单实例中
        article_post_from = ArticlePostForm(request.POST, request.FILES)
        # 判断提交的数据是否满足模型的要求
        if article_post_from.is_valid():
            # 保存数据,但暂时不提交到数据库中
            new_article = article_post_from.save(commit=False)
            # 指定数据库中 id=1 的用户为作者
            # 如果你进行过删除数据表的操作,可能会找不到id=1的用户
            # 此时请重新创建用户,并传入此用户的id
            new_article.author = User.objects.get(id=request.user.id)
            if request.POST['column'] != 'none':
                new_article.column = ArticleColumn.objects.get(
                    id=request.POST['column'])
            # 将新文章保存到数据库中
            new_article.save()
            # 新增代码,保存 tags 的多对多关系
            article_post_from.save_m2m()
            # 完成后返回到文章列表
            return redirect('article:article_list')
        else:
            # 如果数据不合法,返回错误信息
            return HttpResponse('表单内容有误,请重新填写!!')
    else:
        # 创建表单类实例
        columns = ArticleColumn.objects.all()
        article_post_from = ArticlePostForm()
        return render(request, 'article/create.html', locals())
Пример #3
0
def article_create(request):
    categorys = Category.objects.all()
    if request.method == "POST":
        article_post_form = ArticlePostForm(request.POST,request.FILES)
        if article_post_form.is_valid():
            new_article = article_post_form.save(commit=False)
            new_article.author = User.objects.get(id=request.user.id)
            if request.POST['category'] != 'none':
                new_article.category = Category.objects.get(id=request.POST['category'])
            new_article.save()
            article_post_form.save_m2m()
            return redirect("article:article_list")
        else:
            return render(request, 'article/create.html',{ 'categorys':categorys})
    else:
        return render(request, 'article/create.html',{ 'categorys':categorys})
Пример #4
0
def article_edit(request, pid):
    article_obj = ArticlePost.objects.filter(id=pid).first()
    if request.method == 'GET':
        article_form = ArticlePostForm(instance=article_obj)
        context = {'article_form': article_form}
        return render(request, 'article/create.html', context)
    article_form = ArticlePostForm(data=request.POST,
                                   instance=article_obj,
                                   files=request.FILES)
    if article_form.is_valid():
        new_article_obj = article_form.save(commit=False)
        new_article_obj.author = User.objects.get(pk=request.user.id)
        new_article_obj.save()
        article_form.save_m2m()
        # return redirect(reverse('article:article_edit', kwargs={'pid': pid}))
        return redirect('article:article_detail', pid=pid)
    else:
        return HttpResponse('编辑错误')
Пример #5
0
def article_create(request):
    """
    创建博客
    :param request:
    :return:
    """
    if request.method == 'POST':
        article_form = ArticlePostForm(data=request.POST, files=request.FILES)
        if article_form.is_valid():
            new_article_obj = article_form.save(commit=False)
            new_article_obj.author = User.objects.get(
                pk=request.user.id)  # 给博客安排一个作者
            new_article_obj.save()
            article_form.save_m2m()
            return redirect('article:list')
        else:
            return HttpResponse('提交错误')
    else:
        column = ArticleColumn.objects.all()  # 获取栏目数据,用于前端页面展示
        article_form = ArticlePostForm()
        context = {'article_form': article_form, 'columns': column}
        return render(request, 'article/create.html', context)