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 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 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 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_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 })