def addArticle(request): """ 添加文章 :param request: :return: """ if request.method == "GET": article_forms = ArticleForm() return render(request, "addArticle.html", {"article_forms": article_forms}) else: article_forms = ArticleForm(data=request.POST) if article_forms.is_valid(): title = article_forms.cleaned_data.get("title") content = article_forms.cleaned_data.get("content") article_obj = models.Article.objects.create( title=title, desc=content[30], create_time=datetime.datetime.now(), user=request.user) models.ArticleDetail.objects.create(content=content, article=article_obj) else: pass return HttpResponse("添加成功")
def wangzhe(request): if request.method == "GET": obj = ArticleForm() return render(request, 'wangzhe.html', {'obj': obj}) else: obj = ArticleForm(request.POST) if obj.is_valid(): content = obj.cleaned_data['content'] global CONTENT CONTENT = content print(content) return HttpResponse('...')
def wangzhe(request): if request.method == "GET": obj = ArticleForm() return render(request, "wangzhe.html", {"obj": obj}) else: obj = ArticleForm(request.POST) if obj.is_valid(): print(111111111111111) msg = obj.cleaned_data["content"] print(msg) # msg=9999999999999 return render(request, "see.html", {"msg": msg})
def article_add(request): form_obj = ArticleForm() if request.method == 'POST': form_obj = ArticleForm(request.POST) if form_obj.is_valid(): # form_obj.save() # 这里不能用save的原因是因为用了exclude除去了detail,所以直接save的话,detail的信息是无法保存进去的 # 插入文章详情 detail = request.POST.get('detail') detail_obj = models.ArticleDetail.objects.create(content=detail) # 插入文章那张表,这时候文章表少一个detail_id,所以要单独给赋值 form_obj.instance.detail_id = detail_obj.pk form_obj.instance.save() return redirect('article_list') return render(request, 'article_add.html', {'form_obj': form_obj})
def article_edit(request, pk): obj = models.Article.objects.filter(pk=pk).first() form_obj = ArticleForm(instance=obj) article_detail_form_obj = ArticleDetailForm(instance=obj.detail) if request.method == 'POST': form_obj = ArticleForm(request.POST, instance=obj) article_detail_form_obj = ArticleDetailForm(request.POST, instance=obj.detail) if form_obj.is_valid() and article_detail_form_obj.is_valid(): # form_obj.instance.detail.content = request.POST.get('detail') # form_obj.instance.detail.save() # 保存文章详情 article_detail_form_obj.save() form_obj.save() # 保存文章的信息 url = request.GET.get('url') print(request.GET) print(url) if url: return redirect(url) return redirect('article_list') return render(request, 'article_edit.html', { 'form_obj': form_obj, 'article_detail_form_obj': article_detail_form_obj })
def manage_add(request): login_obj = models.UserInfo.objects.filter( username=request.session.get('username')).first() blog_obj = models.UserInfo.objects.filter( username=request.session.get('username')).first() print('z', blog_obj.nid) if request.method == 'GET': obj = ArticleForm(val=login_obj) return render(request, 'manage_add.html', { 'login_obj': login_obj, 'obj': obj, }) else: obj = ArticleForm(login_obj, request.POST) if obj.is_valid(): print('ok', obj.cleaned_data) obj.cleaned_data['read_count'] = 0 obj.cleaned_data['comment_count'] = 0 obj.cleaned_data['up_count'] = 0 obj.cleaned_data['down_count'] = 0 obj.cleaned_data['blog_id'] = blog_obj.nid tags = obj.cleaned_data.pop('tags') content = obj.cleaned_data.pop('content') print(obj.cleaned_data) res = models.Article.objects.create(**obj.cleaned_data) for row in tags: print(res.nid, row) models.Article2Tag.objects.create(article_id=res.nid, tag_id=row) models.ArticleDetail.objects.create(content=content, article_id=res.nid) return redirect('/manage/all.html') else: print('not ok', obj.cleaned_data) return render(request, 'manage_add.html', {'obj': obj})
def article_edit(request, pk): obj = models.Article.objects.filter(pk=pk).first() form_obj = ArticleForm(instance=obj) if request.method == 'POST': form_obj = ArticleForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.instance.detail.content = request.POST.get('detail') form_obj.save() return render(request, 'article_edit.html', {'form_obj': form_obj, 'obj': obj})
def article_add(request): form_obj = ArticleForm() if request.method == 'POST': form_obj = ArticleForm(request.POST) if form_obj.is_valid(): detail = request.POST.get('detail') detail_obj = models.ArticleDetail.objects.create(content=detail) form_obj.instance.detail_id = detail_obj.pk form_obj.save() return redirect('article_list') return render(request, 'article_add.html', {'form_obj': form_obj})
def addArticle(request): user_id = request.user.id if request.method == "POST": form = ArticleForm(user_id, request.POST) print(form) if form.is_valid(): create_time = datetime.datetime.now() data = form.cleaned_data title = data.get("title") tag_ids = data.pop("tag_ids") content = data.get("content") category_id = data.get("category_id") desc = data.get("content")[:30] siteArticleCategory_id = data.get("siteArticleCategory_id") # print(data.get("title")) # print(data.get("desc")) # print(data.get("category_id")) # print(data.get("tag_ids")) # print(data.get("siteArticleCategory_id")) article = models.Article.objects.create( title=title, desc=desc, category_id=category_id, siteArticleCategory_id=siteArticleCategory_id, user_id=user_id) models.ArticleDetail.objects.create(article=article, content=content) if tag_ids: for id in tag_ids: models.Article2Tag.objects.create(article_id=article.id, tag_id=id) return render(request, "updateSuccess.html") return render(request, "addArticle.html", {"form": form}) form = ArticleForm(user_id) return render(request, "addArticle.html", {"form": form})
def article_edit(request, pk): obj = models.Article.objects.filter( pk=pk).first() # 查到一个queryset对象 first拿到第一个 如果没有则返回为null form_obj = ArticleForm(instance=obj) # 传了个instance对象 if request.method == 'POST': form_obj = ArticleForm(request.POST, instance=obj) # 包含数据库没修改之前的数据 以及表单提交的数据 if form_obj.is_valid(): # 通过校验 form_obj.instance.detail.content = request.POST.get('detail') form_obj.instance.detail.save() # 修改以后 还要保存 文章详情 form_obj.save() # 保存文章的信息 return render(request, 'article_edit.html', { 'form_obj': form_obj, 'obj': obj })
def article_add(request): obj = models.Article(author=request.user_obj) form_obj = ArticleForm(instance=obj) # 什么都不传 if request.method == 'POST': form_obj = ArticleForm(request.POST, instance=obj) # 拿到form表单 # 开始校验 if form_obj.is_valid(): # 获取文章详情字符串 detail = request.POST.get('detail') # 创建文章详情对象 detail_obj = models.ArticleDetail.objects.create(content=detail) form_obj.instance.detail_id = detail_obj.pk form_obj.save() # form_obj.instance.save() return redirect('article_list') return render(request, 'article_add.html', {'form_obj': form_obj})
def editArticle(request): user_id = request.user.id article_id = request.GET.get("id") article = models.Article.objects.filter(id=article_id).first() print(article) if request.method == "GET": content = article.articledetail.content tags = article.tags.all() category_id = article.category_id tag_ids = [tag.id for tag in tags] siteArticleCategory_id = article.siteArticleCategory_id form = ArticleForm(user_id, initial={ "title": article.title, "content": content, "category_id": category_id, "tag_ids": tag_ids, "siteArticleCategory_id": siteArticleCategory_id }) return render(request, "editArticle.html", { "form": form, "article_id": article_id }) else: form = ArticleForm(user_id, request.POST) print(form) if form.is_valid(): id = request.GET.get("id") content = form.cleaned_data.pop("content") form.cleaned_data["desc"] = content[:120] print(form.cleaned_data["desc"]) tag_ids = form.cleaned_data.pop("tag_ids") article = models.Article.objects.filter(id=id).first() models.Article.objects.filter(id=id).update(**form.cleaned_data) models.ArticleDetail.objects.filter(article_id=id).update( content=content) tags = [] article.tags.clear() for tag_id in tag_ids: tag = models.Tag.objects.filter(id=tag_id).first() models.Article2Tag.objects.create(article_id=article.id, tag_id=tag_id) return redirect("/backManage/" + request.user.username + "/manage/")
def article_add(request): article_obj = models.Article(author=request.user_obj) form_obj = ArticleForm(instance=article_obj) article_detail_form_obj = ArticleDetailForm() if request.method == 'POST': form_obj = ArticleForm(request.POST, instance=article_obj) article_detail_form_obj = ArticleDetailForm(request.POST) if form_obj.is_valid() and article_detail_form_obj.is_valid(): # detail = request.POST.get("detail") # detail_obj = models.ArticleDetail.objects.create(content=detail) detail_obj = article_detail_form_obj.save() form_obj.instance.detail_id = detail_obj.pk form_obj.save() return redirect('article_list') return render(request, 'article_add.html', { 'form_obj': form_obj, 'article_detail_form_obj': article_detail_form_obj })
def article_add(request): form_obj = ArticleForm() article_detail_form = ArticleDetailForm() if request.method == 'POST': form_obj = ArticleForm(request.POST) article_detail_form = ArticleDetailForm(request.POST) if form_obj.is_valid() and article_detail_form.is_valid(): detail_obj = article_detail_form.save() form_obj.cleaned_data['detail_id'] = detail_obj.pk models.Article.objects.create(**form_obj.cleaned_data) return redirect('article_list') return render(request, "article_add.html", { 'form_obj': form_obj, "article_detail_form": article_detail_form })
def article_edit(request, pk): article_obj = models.Article.objects.filter(pk=pk).first() form_obj = ArticleForm(instance=article_obj) article_detail_form = ArticleDetailForm(instance=article_obj.detail) if request.method == "POST": form_obj = ArticleForm(request.POST, instance=article_obj) article_detail_form = ArticleDetailForm(request.POST, instance=article_obj.detail) if form_obj.is_valid() and article_detail_form.is_valid(): # form_obj.instance.detail.content = request.POST.get('detail') # form_obj.instance.detail.save() article_detail_form.save() form_obj.save() return redirect('article_list') return render( request, 'article_edit.html', { 'article_obj': article_obj, 'form_obj': form_obj, 'article_detail_form': article_detail_form })
def new_article(request, site): if request.method == "GET": print(request.session.get("username")) print(request.session.get("password")) print(request.session.get("id")) msg = request.session.get("username") if not msg: return redirect("/home/") obj = ArticleForm() blog = models.Blog.objects.filter(site=site).first() current_obj = models.UserInfo.objects.filter(nid=blog.user.nid).first() followers = models.UserFans.objects.filter( user_id=current_obj.nid).count() # 粉丝数量 users = models.UserFans.objects.filter( follower_id=current_obj.nid).count() # 关注数量 tag_list = models.Article.objects.filter(blog=blog).values( "tags__nid", "tags__title").annotate(c=Count("nid")) category_list = models.Article.objects.filter(blog_id=blog.nid).values( "category_id", "category__title", ).annotate(c=Count("nid")) type_choice_list = models.Article.type_choices # print(type_choice_list) categorys = [] for i in tag_list: if i["tags__title"]: categorys.append(i) return render( request, "new_article.html", { "obj": obj, "site": site, "current_obj": current_obj, "tag_list": tag_list, "category_list": category_list, "followers": followers, "users": users, "categorys": categorys, "type_choice_list": type_choice_list, }) else: obj = ArticleForm(request.POST) msg = request.session.get("username") if not msg: return redirect("/home/") if obj.is_valid(): content = obj.cleaned_data["content"] summary = content[:30] title = obj.cleaned_data["title"] tag = request.POST.get("tag") if request.POST.get("tag") else 0 type = request.POST.get("type") if request.POST.get("type") else 0 blog_id = request.session.get("id") artic_obj = models.Article.objects.create( title=title, summary=summary, read_count=0, comment_count=0, up_count=0, down_count=0, article_type_id=tag, blog_id=blog_id, category_id=type, ) models.ArticleDetail.objects.create( content=content, article_id=artic_obj.nid, ) return render(request, "see.html", {"content": content})