def article_update(request, id): """ 更新文章的视图函数 通过 POST方法提交表单,更新title,body字段 GET方法进入初始表单页面 id:是文章的id """ # 获取需要修改的具体文章对象 article = ArticlePost.objects.get(id=id) # 判断用户是否为POST提交表单数据 if request.method == "POST": article_post_form = ArticlePostForm(data=request.POST) # 判断提交的数据是否满足模型的需求 if article_post_form.is_valid(): article.title = request.POST.get('title') article.body = request.POST.get('body') article.save() # 完成后返回到修改后的文章中,需要传入文章的id值 return redirect("article:article_detail", id=id) else: return HttpResponse('表单内容有误,请重新填写') # 如果是get请求 else: article_post_form = ArticlePostForm() context = {'article': article, 'article_post_form': article_post_form} # 将响应返回到模板中 return render(request, 'update.html', context)
def article_update(request, id): # 这个拿的文章id 误认为user id了 u_id = request.session.get('user_id') post = ArticlePost.objects.get(id=id) if not u_id == post.author_id: return HttpResponse('无权修改文章') article = ArticlePost.objects.get(id=id) 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(): article.title = request.POST.get('title') article.body = request.POST.get('body') article.category = article_post_form.cleaned_data.get('category') # 删除原来的标签 old_tag = article.tag.all() article.tag.remove(*old_tag) # 这个删除了原来的标签 for i in article_post_form.cleaned_data.get('tag'): article.tag.add(i) article.save() return redirect('article:article_detail', id=id) else: return HttpResponse('输入数据有误') else: article_post_form = ArticlePostForm() # context = { # 'article':article, # # 'article_post_form':article_post_form, # } return render(request, 'article/update.html', locals())
def article_update(request, article_id): """ 更新文章的视图函数 通过POST方法提交表单,更新titile、body字段 GET方法进入初始表单页面 id: 文章的 id """ # 获取需要修改的具体文章对象 article = ArticlePost.objects.get(id=article_id) # 判断用户是否为 POST 提交表单数据 if request.method == "POST": # 将提交的数据赋值到表单实例中 article_post_form = ArticlePostForm(data=request.POST) # 判断提交的数据是否满足模型的要求 if article_post_form.is_valid(): # 保存新写入的 title、body 数据并保存 article.title = request.POST['title'] article.body = request.POST['body'] article.save() # 完成后返回到修改后的文章中。需传入文章的 id 值 return redirect("article:article_detail", article_id=article_id) # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户 GET 请求获取数据 else: # 创建表单类实例 article_post_form = ArticlePostForm() # 赋值上下文,将 article 文章对象也传递进去,以便提取旧的内容 data = {'article': article, 'article_post_form': article_post_form} # 将响应返回到模板中 return render(request, 'article/update.html', data)
def article_update(request, id): categorys = Category.objects.all() article = ArticlePost.objects.get(id=id) if request.method == "POST": article_post_form = ArticlePostForm(request.POST) if article_post_form.is_valid(): article.title = request.POST['title'] article.content = request.POST['content'] article.save() return redirect("article:article_detail", id=id) else: article_post_form = ArticlePostForm() return render(request, 'article/update.html',{ 'article': article, 'article_post_form': article_post_form,'categorys':categorys})
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)
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())
def article_update(request, id): """ 更新文章的视图函数 通过POST方法提交表单,更新titile、body字段 GET方法进入初始表单页面 id: 文章的 id """ # 获取需要修改的具体文章对象 article = ArticlePost.objects.get(id=id) # 过滤非作者且不是超级管理员的用户 if request.user != article.author and (not request.user.is_superuser): return HttpResponse("抱歉,你无权修改这篇文章。") if request.method == 'POST': article_post_form = ArticlePostForm(data=request.POST) if article_post_form.is_valid(): # 保存数据,但暂时不提交到数据库中 article.title = request.POST['title'] article.body = request.POST['body'] if request.POST['column'] != 'none': article.column = ArticleColumn.objects.get( id=request.POST['column']) else: article.column = None tags = request.POST['tags'] if tags.strip() != '': tags_arr = tags.replace(' ', '').split(',') article.tags.set(*tags_arr, clear=False) else: article.tags.clear() # 保存article article.save() return redirect('article:article-detail', id=id) else: return HttpResponse("表单内容有误,请重新填写。") else: # 创建表单类实例 article_post_form = ArticlePostForm() columns = ArticleColumn.objects.all() # 赋值上下文 context = { 'article': article, 'article_post_form': article_post_form, 'columns': columns } # 返回模板 return render(request, 'article/update.html', context)
def edit_article(req, article_id): if req.method == 'GET': # print(dir(req.user),article_id) article_columns = req.user.article_column.all() article = ArticlePost.objects.get(id=article_id) this_article_form = ArticlePostForm(initial={'title': article.title}) this_article_column = article.column return render( req, 'article/column/edit_article.html', { 'article_columns': article_columns, 'article': article, 'this_article_form': this_article_form, 'this_article_column': this_article_column }) else: edit_article = ArticlePost.objects.get(id=article_id) try: edit_article.column = req.user.article_column.get( id=req.POST['column_id']) edit_article.title = req.POST['title'] edit_article.body = req.POST['body'] edit_article.save() return HttpResponse('1') except: return HttpResponse('2')
def redit_article(request, article_id): if request.method == "GET": article_columns = request.user.article_column.all() article = ArticlePost.objects.get(id=article_id) this_article_form = ArticlePostForm(initial={"title": article.title}) this_article_column = article.column article_tags = request.user.tag.all() return render( request, "article/column/redit_article.html", { "article": article, "article_columns": article_columns, "this_article_form": this_article_form, "this_article_column": this_article_column, "article_tags": article_tags }) else: redit_article = ArticlePost.objects.get(id=article_id) try: redit_article.column = request.user.article_column.get( id=request.POST['column_id']) redit_article.title = request.POST['title'] redit_article.body = request.POST['body'] redit_article.save() tags = request.POST['tags'] if tags: for atag in json.loads(tags): tag = request.user.tag.get(tag=atag) redit_article.article_tag.add(tag) return HttpResponse("1") except: return HttpResponse("2")
def redit_article(request, article_id): if request.method == 'GET': article_columns = request.user.article_column.all() article = ArticlePost.objects.get(id=article_id) this_article_form = ArticlePostForm(initial={'title': article.title}) this_article_column = article.column return render( request, 'article/redit_article.html', { 'article': article, 'article_columns': article_columns, 'this_article_form': this_article_form, 'this_article_column': this_article_column }) else: print(request.POST) redit_article = ArticlePost.objects.get(id=article_id) print(article_id) try: redit_article.column = request.user.article_column.get( id=request.POST['column_id']) redit_article.title = request.POST['title'] redit_article.body = request.POST['body'] redit_article.save() return HttpResponse('1') except: return HttpResponse('0')
def article_update(request, id): """ 更新文章的视图函数 通过POST方法提交表单,更新title、body字段 GET方法进入初始表单页面 :param request: :param id: 文章的id :return: """ # 获取需要修改的具体文章的对象 article = ArticlePost.objects.get(id=id) # 过滤非法用户 if request.user != article.author: return HttpResponse('很抱歉,您无权修改本文章') # 判断用户是否为POST提交表单数据 if request.method == 'POST': # 将提交的数据赋值到表单实例中 ''' 另一种方法: article_post_form = ArticlePostForm(data=request.POST, instance=article) if article_post_form.is_valid(): article_post_form.save() ''' article_post_form = ArticlePostForm(data=request.POST) # 判断提交的数据是否满足模型的要求 if article_post_form.is_valid(): # 保存新写入的title、body数据并保存 article.title = request.POST['title'] article.body = request.POST['body'] article.save() # 完成后返回到修改的文章中,需传入文章的id值 return redirect('article:article_detail', id=id) # 数据不合法,返回错误信息 else: return HttpResponse('表单信息有误,请重新填写') # 如果用户GET请求获取数据 else: # 创建表单实例 article_post_form = ArticlePostForm() # 赋值上下文,将article文章对象也传递进去,以便提取旧的内容 context = { 'article': article, 'article_post_form': article_post_form, } return render(request, 'article/update.html', context)
def article_post(request): if request.method == "POST": form = ArticlePostForm(request.POST) if form.is_valid(): res = ArticlePost.objects.create( author=request.user, title=request.POST.get('title').strip(), column=ArticleColumn.objects.get(pk=request.POST.get('column').strip()), body=request.POST.get('body').strip() ) return JsonResponse({'code': 200, 'url': reverse('article:article_detail', args=[res.id, res.slug])}) else: return JsonResponse({"code": 400, "error": form.errors}) elif request.method == "GET": form = ArticlePostForm() article_col = request.user.article_column.all() return render(request, 'article/article_post.html', {'article_column': article_col, 'form': form})
def article_update(request, id): """ 更新文章的视图函数 通过POST方法提交表单,更新titile、body字段 GET方法进入初始表单页面 id: 文章的 id """ print(request.user) # 获取需要修改的具体文章对象 article = ArticlePost.objects.get(id=id) # 过滤非作者的用户 if article.author != request.user: return HttpResponse("抱歉,你无权修改这篇文章。") # 判断用户是否为 POST 提交表单数据 if request.method == 'POST': # 将提交的数据赋值到表单实例中 article_post_form = ArticlePostForm(data=request.POST) # 判断提交的数据是否满足模型的要求 if article_post_form.is_valid(): # 保存新写入的 title、body 数据并保存 article.title = article_post_form.cleaned_data['title'] article.body = article_post_form.cleaned_data['body'] # article.title = request.POST['title'] # article.body = request.POST['body'] if request.POST['column'] != 'none': article.column = ArticleColumn.objects.get( id=request.POST['column']) else: article.column = None if request.FILES.get('avatar'): article.avatar = request.FILES.get('avatar') article.tags.set(*request.POST.get('tags').split(','), clear=True) article.save() # 完成后返回到修改后的文章中。需传入文章的 id 值 return redirect('article:article_detail', id=id) else: # 如果数据不合法,返回错误信息 return HttpResponse('表单填写错误,请重新填写!!') else: # 如果用户 GET 请求获取数据 article_post_from = ArticlePostForm() columns = ArticleColumn.objects.all() tags = ','.join([x for x in article.tags.names()]) return render(request, 'article/update.html', locals())
def edit_article(request, article_id): if request.method == 'GET': article_columns = request.user.article_column.all() article = ArticlePost.objects.get(id=article_id) article_tags = request.user.tag.all() article_tags_chose = article.article_tag.all() this_article_form = ArticlePostForm(initial={ 'title': article.title, 'body': article.body, }) this_article_column = article.column return render( request, 'article/column/article_post.html', { 'article_post_form': this_article_form, 'article_columns': article_columns, 'this_article_column': this_article_column, 'article_tags': article_tags, 'article_tags_chose': article_tags_chose }) if request.method == 'POST': article = get_object_or_404(ArticlePost, id=article_id) edit_article_form = ArticlePostForm(data=request.POST) if edit_article_form.is_valid(): cd = edit_article_form.cleaned_data try: article.title = cd['title'] article.body = cd['body'] article.column = request.user.article_column.get( id=request.POST['column_id']) article.save() tag_id_list = request.POST.getlist('article_tag') tags_chose = article.article_tag.all() for tag in tags_chose: article.article_tag.remove(tag) for tag_id in tag_id_list: tag = ArticleTag.objects.get(id=tag_id) article.article_tag.add(tag) return redirect('article:article_detail', article_id, article.slug) except: return HttpResponse('error') return HttpResponse('error2')
def article_update(request, article_id): article = ArticlePost.objects.get(id=article_id) # 过滤非作者的用户 if request.user != article.author: return HttpResponse("抱歉,你无权修改这篇文章。") if request.method == "POST": article_post_form = ArticlePostForm(data=request.POST) if article_post_form.is_valid(): article.title = request.POST['title'] article.body = request.POST['body'] article.save() return redirect("article:article_list") else: return HttpResponse("表单内容有误,请重新填写。") else: # 创建表单类实例 article_post_form = ArticlePostForm() # 赋值上下文,将 article 文章对象也传递进去,以便提取旧的内容 context = {'article': article, 'article_post_form': article_post_form} # 将响应返回到模板中 return render(request, 'article/update.html', context)
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中可以用到
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 })
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, })
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 })
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)
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)
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})
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('写文章前必须先登陆')
def article_update(request, id): """更新文章""" # 同post方法提交表单,更新的字段有 title、body article = ArticlePost.objects.get(pk=id) if request.user != article.author: return HttpResponse("抱歉,你无权修改这篇文章。") if request.method == 'POST': # 如果有post过来数据 article_post_form = ArticlePostForm( data=request.POST) # 如果是post就将提交的数据赋值到实例表单中 if article_post_form.is_valid(): # 判断提交数据符合模型要求 article.title = request.POST['title'] # 将新数据覆盖原来的数据,title article.body = request.POST['body'] article.save() # 保存数据库 return redirect('article:article_detail', id=id) # 修改成功后跳转到文章详情页面 else: # 如果提交数据不合法就给提示 return HttpResponse("表单内容有误,请重新填写") else: # 用Get方法 获取旧数据 article_post_form = ArticlePostForm() # 创建实例化表单 context = { 'article': article, 'article_post_form': article_post_form, } return render(request, 'article/update.html', context=context)
def edit_article_post(request, pk): post = get_object_or_404(ArticlePost, pk=pk) if request.method == 'POST': form = ArticlePostForm(request.POST) if form.is_valid(): post.body = request.POST.get('body').strip() post.title = request.POST.get('title').strip() post.column = ArticleColumn.objects.get(pk=request.POST.get('column').strip()) post.save() return JsonResponse({ "code": 200, "msg": 'Updated', 'url': reverse('article:article_detail', args=[post.id, post.slug]) }) else: return JsonResponse({"code": 400, "msg": str(form.errors)}) elif request.method == 'GET': article_col = request.user.article_column.all() return render(request, 'article/edit_article_post.html', {"post": post, 'article_column': article_col}) else: post.delete() return JsonResponse({"code": 200, 'msg': "Deleted"})
def article_update(request, pk): """ 通过POST方法提交表单,更改title,body字段内容 通过GET方法进入初始表单页面 :param request: POST,GET :param pk: article.pk :return: 修改后的文字详情页 """ # user=User.objects.get(is_superuser=1) article = get_object_or_404(ArticlePost, pk=pk) if request.method == 'POST': if request.user != article.author: return HttpResponse("你没有权限修改文章。") # 得到提交数据的表单实例 article_post_form = ArticlePostForm(request.POST) # 验证是否合法 if article_post_form.is_valid(): # 合法,则把修改的内容得到 article.title = article_post_form.cleaned_data['title'] article.body = article_post_form.cleaned_data['body'] article.tags = article_post_form.cleaned_data['tags'] article.category = article_post_form.cleaned_data['category'] article.save() # 完成修改后回到修改后的文章 return redirect('article:article_detail', pk=pk) # 不合法 else: return HttpResponse('表单内容有误,请重新填写') # GET请求获取数据 else: article_post_form = ArticlePostForm() return render(request, 'article/update.html', { 'article': article, 'article_post_form': article_post_form, })
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('编辑错误')
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)
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})
def redit_article(request, article_id): if request.method == "GET": article_columns = request.user.article_column.all() article = ArticlePost.objects.get(id=article_id) this_article_form = ArticlePostForm(initial={"title": article.title}) return render( request, "article/column/redit_article.html", { "article": article, "article_columns": article_columns, "this_article_form": this_article_form }) else: redit_article = ArticlePost.objects.get(id=article_id) try: redit_article.column = request.user.article_column.get( id=request.POST['column_id']) redit_article.title = request.POST['title'] redit_article.body = request.POST['body'] redit_article.save() return HttpResponse("1") except: return HttpResponse("2")