Пример #1
0
def article_create(request):
    # 判断提交方式
    if request.method == 'POST':
        # 将提交的数据赋值到表单实例中
        article_post_form = ArticlePostForm(request.POST)
        # 验证表单是否合法
        if article_post_form.is_valid():
            # 合法,保存数据但不保存到数据库
            # 得到实例的具体对象----文章
            new_article = article_post_form.save(commit=False)
            # 指定文章的作者
            new_article.author = User.objects.get(pk=request.user.pk)

            # 将新文章保存到数据库
            new_article.save()
            # 发表完文章,重定向到文章列表
            return redirect('article:article_list')
        # 如果表单数据不合法
        else:
            return HttpResponse('表单内容有误,请重新填写')

    # GET请求
    else:
        article_post_form = ArticlePostForm()
    return render(request, 'article/create.html', {
        'article_post_form': article_post_form,
    })
Пример #2
0
def article_post(request):
    if request.method == "POST":
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            cd = article_post_form.cleaned_data
            try:
                new_article = article_post_form.save(
                    commit=False)  # 此时还未上传到数据库
                new_article.author = request.user
                new_article.column = request.user.article_column.get(
                    id=request.POST['column_id'])
                new_article.save()
                tags = request.POST['tags']  # tags以json包形式传递
                if tags:
                    for atag in json.loads(tags):
                        tag = request.user.tag.get(tag=atag)
                        new_article.article_tag.add(tag)
                return HttpResponse("成功上传")
            except:
                return HttpResponse("表单有效,但上传失败")
        else:
            return HttpResponse("表单无效")
    else:
        article_post_form = ArticlePostForm()
        article_columns = request.user.article_column.all()
        article_tags = request.user.tag.all()
        return render(
            request, "article/column/article_post.html", {
                "article_post_form": article_post_form,
                "article_columns": article_columns,
                "article_tags": article_tags
            })  # 此处传参html中可以用到
Пример #3
0
def articled_create(request):
    # 判断用户是否提交数据
    if request.method == 'POST':
        # 将提交的数据赋值到表单实例中
        article_post_form = ArticlePostForm(data=request.POST)
        # 判断提交的数据是否满足模型的要求
        if article_post_form.is_valid():
            # 保存数据,但暂时不提交到数据库中
            new_article = article_post_form.save(commit=False)
            # 指定作者为当前登录用户
            new_article.author = User.objects.get(id=request.user.id)
            # 将新文章保存在数据库中
            new_article.save()
            # 完成后返回文章列表
            return redirect('article:article_list')
        # 如果数据不合法,返回错误信息
        else:
            return HttpResponse('表单内容有误,请重新填写!!!')

    # 如果用户请求获取数据
    else:
        # 创建表单类实例
        article_post_form = ArticlePostForm()
        # 赋值上下文
        context = {
            'article_post_form': article_post_form,
        }
        return render(request, 'article/create.html', context)
Пример #4
0
def article_create(request):
    # 判断用户是否提交数据
    if request.method == "POST":
        # 将提交的数据赋值到表单实例中
        article_post_form = ArticlePostForm(data=request.POST)
        # 判断提交的数据是否满足模型的要求
        if article_post_form.is_valid():
            # 保存数据,但暂时不提交到数据库中
            new_article = article_post_form.save(commit=False)
            # 指定数据库中 id=1 的用户为作者
            # 如果你进行过删除数据表的操作,可能会找不到id=1的用户
            # 此时请重新创建用户,并传入此用户的id
            new_article.author = User.objects.get(id=1)
            # 将新文章保存到数据库中
            new_article.save()
            # 完成后返回到文章列表
            return redirect("article:article_list")
        # 如果数据不合法,返回错误信息
        else:
            return HttpResponse("表单内容有误,请重新填写。")
    # 如果用户请求获取数据
    else:
        # 创建表单类实例
        article_post_form = ArticlePostForm()
        # 赋值上下文
        context = {'article_post_form': article_post_form}
        # 返回模板
        return render(request, 'article/create.html', context)
Пример #5
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)
Пример #6
0
def article_post(request):
    if request.method == 'POST':
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            cd = article_post_form.cleaned_data
            try:
                new_article = article_post_form.save(commit=False)
                new_article.author = request.user
                new_article.column = request.user.article_column.get(id=request.POST['column_id'])
                new_article.save()
                tags = request.POST['tags']
                if tags:
                    for atag in json.loads(tags):
                        tag = request.user.tag.get(tag=atag)
                        new_article.article_tag.add(tag)
                return HttpResponse("1")
            except:
                return HttpResponse("2")
        else:
            return HttpResponse("3")
    else:
        article_post_form = ArticlePostForm()
        article_columns = request.user.article_column.all()
        article_tags = request.user.tag.all()
        return render(request, "article/column/article_post.html", {"article_post_form": article_post_form,
                                                                    "article_columns": article_columns,
                                                                    "article_tags": article_tags})
Пример #7
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())
Пример #8
0
def article_create(request):
    if request.session.get('is_login', None):
        category_list = Category.objects.all()
        tag_list = Tag.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)
                uid = request.session.get('user_id')
                new_article.author = User.objects.get(id=uid)
                new_article.category = article_post_form.cleaned_data.get(
                    'category')
                new_article.save()
                # 这个save()是因为没有文章的id是无法建立多对多操作的
                for i in article_post_form.cleaned_data.get('tag'):
                    new_article.tag.add(i)
                new_article.save()
                return redirect('article:article_list')
            else:
                return HttpResponse('表单数据有误,请重新填写')
        else:
            article_post_form = ArticlePostForm()
            # context = {
            #     'article_post_form': article_post_form
            # }
            return render(request, 'article/create.html', locals())
    else:
        return HttpResponse('写文章前必须先登陆')
Пример #9
0
def articlepost(req):
    if req.method == 'POST':
        article_post_form = ArticlePostForm(req.POST)
        if article_post_form.is_valid():
            #                 cd = article_post_form.cleaned_data
            try:
                new_article = article_post_form.save(commit=False)
                new_article.author = req.user
                print(new_article.author, req.POST['column_id'])
                new_article.column = ArticleColumn.objects.get(
                    user=req.user, id=req.POST['column_id'])
                new_article.save()
                return HttpResponse('1')
            except:
                return HttpResponse('0')
        return HttpResponse('2')
    else:
        article_post_form = ArticlePostForm()
        article_columns = ArticleColumn.objects.filter(
            user=req.user
        )  #req.user.article_column.all()  models中有个related——name参数用来查询登录用户的对应models字段Queryset
        myinfo = UserInfo.objects.get(user=req.user)
        return render(
            req, 'article/column/article_post.html', {
                'article_post_form': article_post_form,
                'article_columns': article_columns,
                'userinfo': myinfo
            })
Пример #10
0
def article_post(request):
    if request.method == 'POST':
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            cd = article_post_form.cleaned_data
            try:
                new_article = article_post_form.save(commit=False)
                new_article.author = request.user
                new_article.column = request.user.article_column.get(
                    id=request.POST['column_id'])
                new_article.save()
                tag_id_list = request.POST.getlist('article_tag')
                for tag_id in tag_id_list:
                    tag = ArticleTag.objects.get(id=tag_id)
                    new_article.article_tag.add(tag)
                return redirect('article:article_list')
            except:
                return HttpResponse('2')
        else:
            return HttpResponse('3')
    else:
        article_post_form = ArticlePostForm()
        article_columns = request.user.article_column.all()
        article_tags = request.user.tag.all()
        this_article_column = ''
        return render(
            request, 'article/column/article_post.html', {
                'article_post_form': article_post_form,
                'article_columns': article_columns,
                'this_article_column': this_article_column,
                'article_tags': article_tags
            })
Пример #11
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})
Пример #12
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('编辑错误')
Пример #13
0
def article_create(request):
    # user = User.objects.get(id=article_id)
    # 判断用户是否提交数据
    if request.method == "POST":
        # 将提交的数据赋值到表单中
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            new_article = article_post_form.save(commit=False)
            new_article.author = User.objects.get(id=request.user.id)
            new_article.save()
            return redirect("article:article_list")
        else:
            return HttpResponse("表单内容有误,请重新填写")
    else:
        # 创建表单类实例
        article_post_form = ArticlePostForm()
        # 赋值上下文
        context = {'article_post_form': article_post_form}
        # 返回模板
        return render(request, 'article/create.html', context)
Пример #14
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)
Пример #15
0
def article_create(request):
    """提交表单数据的处理"""
    if request.method == "POST":  # 如果接受到提交数据
        article_post_form = ArticlePostForm(
            data=request.POST)  # 如果是提交了数据就将提交数据赋值给表单实例中
        if article_post_form.is_valid():  # 判断数据是否符合模型要求
            new_article = article_post_form.save(
                commit=False)  # 保存提交的数据,暂不提交到数据库
            new_article.author = User.objects.get(
                id=request.user.id)  # 传入id为1的用户
            # 以上做了操作:首先是将提交的内容赋值给form表单的实例化,然后判断它是不是符合要求,
            # 然后保存将数据暂时保存到内存中,不保存到数据库,最后是把作者也传给他,那么现在数据就是标题,内容,作者

            new_article.save()  # 保存到数据库
            return redirect('article:article_list')  # 最后重定向到首页
        else:
            return HttpResponse('表单内容有误,请重新填写')
    else:  # 如果没有接受到提交的数据,就渲染页面填写
        article_post_form = ArticlePostForm()
        context = {
            'article_post_form': article_post_form,
        }
        return render(request, 'article/create.html', context=context)
Пример #16
0
def article_create(request):
    # 判断用户是否提交数据
    if request.method == "POST":
        # 将提交的数据赋值到表单实例中
        article_post_form = ArticlePostForm(data=request.POST)
        # 判断提交的数据是否满足模型的需求
        if article_post_form.is_valid():
            # 保存数据,但暂时不提交到数据库中
            new_article = article_post_form.save(commit=False)
            # 指定数据库中,id=1的用户为作者
            # 如果你进行过删除数据表的操作,可能会找不到id=1的用户
            # 此时请重新创建用户,并传入此用户id
            new_article.author = User.objects.get(id=1)
            # 将新文章保存到数据库中
            new_article.save()
            return redirect('article:article_list')
        else:
            return HttpResponse('表单内容有误,请重新填写')
    # 如果用户请求获取数据
    else:
        # 创建表单实例
        article_post_form = ArticlePostForm()
        context = {'article_post_form': article_post_form}
        return render(request, 'create.html', context)
Пример #17
0
def article_post(request):
    if request.method == 'POST':
        article_post_form = ArticlePostForm(data=request.POST)
        if article_post_form.is_valid():
            cd = article_post_form.cleaned_data
            try:
                new_article = article_post_form.save(commit=False)
                new_article.author = request.user
                new_article.column = request.user.article_column.get(
                    id=request.POST['column_id'])
                new_article.save()
                return HttpResponse("1")
            except:
                return HttpResponse("2")
        else:
            return HttpResponse("3")
    else:
        article_post_form = ArticlePostForm()
        article_columns = request.user.article_column.all()
        return render(
            request, 'article/article_post.html', {
                'article_post_form': article_post_form,
                'article_columns': article_columns
            })