def update_blog(request, pk): context = {} redirect_to = request.GET.get('from', reverse('blog')) # 判断提交方式 # 首先获取要重新编辑的博客的id article = Blog.objects.filter(pk=pk).first() # 判断是否作者本人操作 if article.author != request.user: context['message'] = '非本人操作!' return render(request, 'error.html', context) if request.method == 'POST': article = Blog.objects.filter(pk=pk).first() article_form = ArticlePostForm(request.POST, request.FILES) # 判断提交数据是否满足模型的要求 if article_form.is_valid(): # 保存新写入的博客内容 article.title = request.POST['title'] article.content = request.POST['content'] article.blog_type = article_form.cleaned_data['blog_type'] article.avatar = article_form.cleaned_data['avatar'] # print(article.avatar) article.save() return redirect('blog_detail', pk) else: context['message'] = '表单填写内容有误,请检查后重新操作!' return render(request, 'error.html', context) else: # get请求 article_form = ArticlePostForm(instance=article) context = {'update_article': article, 'update_form': article_form} context['return_back_href'] = redirect_to return render(request, 'user/update_blog.html', context)
def article_update(request, id): """ 更新文章的视图函数 """ 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.body = request.POST['body'] article.save() if request.POST['column'] != 'None': columns = request.POST.getlist("column") for c in ArticleColumn.objects.all(): if str(c.id) not in columns: article.column.remove(c) article.save() else: article.column.add(c) article.save() return redirect("blog:detail", id=id) else: return HttpResponse("表单内容有误,请重新填写。") else: article_post_form = ArticlePostForm() columns = ArticleColumn.objects.all() context = { "article_post_form": article_post_form, "article": article, "columns": columns } return render(request, "blog/update.html", context=context)
def article_update(request, article_id): # 获取需要修改的具体文章对象 article = Article.objects.get(id=article_id) # 过滤非作者的用户 if request.user != article.user: return HttpResponse("抱歉,你无权修改这篇文章。") # 获取所有的分类 categories = Category.objects.all() # 获取所有的标签 tags = Tag.objects.tag_article_num_list() # 判断用户是否为 POST 提交表单数据 if request.method == "POST": # 将提交的数据赋值到表单实例中 article_post_form = ArticlePostForm(request.POST, request.FILES) # 获取所有被选中的标签 select_tags = request.POST.getlist('tags[]', '') # 判断提交的数据是否满足模型的要求 if article_post_form.is_valid(): # 保存新写入的 title、body 数据并保存 article.title = request.POST['title'] article.content = request.POST['content'] if request.FILES: article.img = request.FILES['img'] article.category = Category.objects.get( id=request.POST['category']) article.save() # 先删掉原文章的标签,再依次加入新的标签 for tag in article.tag.all(): article.tag.remove(tag) for select_tag in select_tags: tag = Tag.objects.get(id=int(select_tag)) article.tag.add(tag) # 完成后返回到修改后的文章中。需传入文章的 id 值 return redirect("blog:detail", article_id=article.id) # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户 GET 请求获取数据 elif request.method == 'GET': # 创建表单类实例 article_post_form = ArticlePostForm() # 赋值上下文,将 article 文章对象也传递进去,以便提取旧的内容 context = { 'article': article, 'article_post_form': article_post_form, 'categories': categories, 'tags': tags, } # 将响应返回到模板中 return render(request, 'blog/article_update.html', context)
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=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']) else: new_article.column = None new_article.save() article_post_form.save_m2m() return redirect('/') #重新定向回主页 # 如果数据不合法,返回错误信息 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/createpage.html', context)
def article_edit(request, id): if request.method == 'POST': pass else: article_post_form = ArticlePostForm() columns = ArticleColumn.objects.all() context = {"article_post_form": article_post_form, "columns": columns} return render(request, "blog/edit.html", context=context)
def user_info(request): context = {} if request.user.is_authenticated: collect_list = CollectRecord.objects.filter(user=request.user) #使用分页器进行处理 context = page(request, collect_list) context['collect_list'] = collect_list if request.method == 'POST': # 将提交的数据赋值给表单实例 article_post_form = ArticlePostForm(request.POST, request.FILES) if article_post_form.is_valid(): # 保存数据但是暂时不提交到数据库中 post_article = article_post_form.save(commit=False) # 指定文章作者为请求方 post_article.author = request.user # 然后保存到数据库 post_article.save() context['post_tip'] = '发送成功' return redirect('blog_detail', post_article.pk) else: context['message'] = article_post_form.errors return render(request, 'error.html', context) else: context['message'] = '请确认是否登录状态' return render(request, 'error.html', context) if request.user.is_superuser: personal_blog_list = Blog.objects.filter(author=request.user) context['personal_blog_list'] = personal_blog_list context['article_form'] = ArticlePostForm() context['login_form'] = LoginForm() context['register_form'] = RegisterForm() context['change_password_form'] = ChangePasswordForm() context['change_nickname_form'] = ChangeNicknameForm() context['bing_email_form'] = BindEmailForm() user = request.user context['all_msg'] = Notification.objects.filter(recipient=user).unread() context['num'] = Notification.objects.filter( recipient=user).unread().count() hot_blogs = get_hot_blog( content_type=ContentType.objects.get_for_model(Blog)) context["hot_blogs"] = hot_blogs return render(request, 'user_info.html', context)
def article_update(request, id): """ 更新文章的视图函数 GET方法进入初始表单页面 id: 文章的 id """ # 获取需要修改的具体文章对象 article = Blog.objects.get(id=id) if request.user == article.author or request.user.is_superuser: # 判断用户是否为 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.content = request.POST['content'] blog_type_id = request.POST['blog_type'] article.blog_type = BlogType.objects.get(id=blog_type_id) article.save() return redirect("blog_detail", id) # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户 GET 请求获取数据 else: article_post_form = ArticlePostForm() # 提取旧的内容 context = {} context['article'] = article context['article_post_form'] = article_post_form context['blog_types'] = BlogType.objects.all() # 将响应返回到模板中 return render(request, 'blog/update.html', context) else: return HttpResponse("无权限。")
def article_post(request): if request.method == 'POST': article_post_form = ArticlePostForm(request.POST) # print(article_post_form) if article_post_form.is_valid(): cd = article_post_form.cleaned_data try: new_article = article_post_form.save(commit=False) # new_article=article_post_form.save(commit=False) # print(new_article+'-----') # # new_article.author=request.user # # new_article.column=request.user.article_column.get(id=request.POST['column_id']) new_article.save() tags = request.POST['tag'] print(type(new_article)) if tags: for atag in json.loads(tags): new_article.tags.add(atag) return HttpResponse(1) #跳转到index界面 except: # print('error-----') return HttpResponse(2) else: return HttpResponse(3) else: article_post_form = ArticlePostForm() article_columns = Category.objects.all() article_tags = Tag.objects.all() return render( request, 'blog/publish_article.html', { 'aritcle_post_form': article_post_form, 'article_coloumns': article_columns, 'article_tags': article_tags })
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 = request.user # 保存到数据库中 new_article.save() # 完成后返回到文章列表 return redirect("blog_list") # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户请求获取数据 else: # 创建表单类实例 article_post_form = ArticlePostForm() # 赋值上下文 context = {} context['article_post_form'] = article_post_form context['blog_types'] = BlogType.objects.all() # 返回模板 return render(request, 'blog/create.html', context)
def article_update(request, id): article = ArticlePost.objects.get(id=id) if request.user == article.author: if request.method == "POST": article_post_form = ArticlePostForm(request.POST, request.FILES) if article_post_form.is_valid(): article.title = request.POST['title'] article.body = request.POST['body'] if request.FILES: article.avatar = request.FILES.get('avatar') tags = request.POST['tags'] if (tags): # print(tags) # print(article.tags.all) # article.tags.clear() # for tag in tags.split(","): # article.tags.add(tag) # print(tag) article.tags.set(*tags.split(","), clear=True) # article.tags.set(request.POST['tags'],clear=True) # print(request.POST['title']) # print(article.body) if (request.POST['column'] != 'none'): article.column = ArticleColumn.objects.get( id=request.POST['column']) else: pass # article.column=None article.save() return redirect("/article/article-detail/" + str(id)) else: return HttpResponse("表???") else: #metho GET article_get_form = ArticlePostForm() columns = ArticleColumn.objects.all() tags = ','.join([x for x in article.tags.names()]) print(article.tags.names()) print(tags) article_get_form.body = article.body context = { 'article': article, 'article_get_form': article_get_form, 'columns': columns, 'tags': tags } return render(request, 'article/createpage.html', context) else: return HttpResponse("无权修改")
def article_add(request): """写文章的视图""" # 获取所有的标签 tags = Tag.objects.tag_article_num_list() # 获取当前用户信息 user = request.user if request.method == 'GET': # 创建表单类实例 article_post_form = ArticlePostForm() # 获取所有的文章分类 categories = Category.objects.all() return render(request, 'blog/article_add.html', context={ 'user': user, 'tags': tags, 'categories': categories, 'article_post_form': article_post_form }) elif request.method == 'POST': # 将提交的数据赋值到表单实例中 article_post_form = ArticlePostForm(request.POST, request.FILES) # 获取提交过来的标签信息 select_tags = request.POST.getlist('tags[]', '') if article_post_form.is_valid(): # 保存数据,但暂时不提交到数据库中 new_article = article_post_form.save(commit=False) # 指定当前登录的用户为作者 new_article.user = BlogUser.objects.get(id=user.id) # 指定文章的分类 new_article.category = Category.objects.get( id=request.POST['category']) # 如果用户没有上传照片,设置为默认图片 if not new_article.img or not hasattr(new_article.img, 'url'): setattr(new_article, 'img', 'default/default.jpg') # 将文章保存到数据库 new_article.save() # 依次加入标签 for select_tag in select_tags: tag = Tag.objects.get(id=int(select_tag)) new_article.tag.add(tag) # 完成后回到首页 return redirect('blog:index') # 如果提交数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。")
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) new_article.author = get(id=request.user.id) # 将新文章保存到数据库 new_article.save() if request.POST['column'] != 'None': columns = request.POST.getlist("column") for c in columns: column = ArticleColumn.objects.get(id=c) new_article.column.add(column) new_article.save() return redirect("blog:index") else: return HttpResponse("表单内容有误,请重新填写。") else: article_post_form = ArticlePostForm() columns = ArticleColumn.objects.all() context = {"article_post_form": article_post_form, 'columns': columns} return render(request, 'blog/create.html', context=context)