def edit_article(request, article_id): if request.method == 'GET': article = Article.objects.get(id=article_id) labels_in_article = article.labels.all() labels = Label.objects.all() return render(request, 'edit_article.html', {'article': article, 'labels': labels, 'labels_in_article': labels_in_article}) elif request.method == 'POST': edit_article = Article.objects.get(id=article_id) edit_article.title = request.POST['article_title'] edit_article.raw_content = request.POST['article_raw_content'] edit_article.markdown_content = request.POST['article_markdown_content'] edit_article.last_edit_time = timezone.now() edit_article.save() # 清楚所有关联label edit_article.labels.clear() # 重新绑定 labels = request.POST.getlist('labels[]') for label_id in labels: label = Label.objects.get(id=label_id) # label.number_is_used = label.number_is_used + 1 label.save() lia = LabelsInArticle(article=edit_article, label=label) lia.save() labels = Label.objects.all() labels_in_article = edit_article.labels.all() return render(request, 'edit_article_form.html', {'article': edit_article, 'labels': labels, 'labels_in_article': labels_in_article})
def add_article(request): if request.method == 'GET': labels = Label.objects.all() return render(request, 'add_article.html', {'labels': labels}) elif request.method == 'POST': article_title = request.POST['article_title'] article_raw_content = request.POST['article_raw_content'] article_markdown_content = request.POST['article_markdown_content'] labels = request.POST.getlist('labels[]') new_article = Article.objects.create(title=article_title, raw_content=article_raw_content, markdown_content=article_markdown_content, post_time=timezone.now(), last_edit_time=timezone.now()) for label_id in labels: label = Label.objects.get(id=label_id) # label.number_is_used = label.number_is_used + 1 label.save() lia = LabelsInArticle(article=new_article, label=label) lia.save() return render(request, 'add_article_form.html', {})
def add_article(request): if request.method == 'GET': labels = Label.objects.all() return render(request, 'add_article.html', {'labels': labels}) elif request.method == 'POST': article_title = request.POST['article_title'] article_raw_content = request.POST['article_raw_content'] article_markdown_content = request.POST['article_markdown_content'] labels = request.POST.getlist('labels[]') new_article = Article.objects.create( title=article_title, raw_content=article_raw_content, markdown_content=article_markdown_content, post_time=timezone.now(), last_edit_time=timezone.now()) for label_id in labels: label = Label.objects.get(id=label_id) # label.number_is_used = label.number_is_used + 1 label.save() lia = LabelsInArticle(article=new_article, label=label) lia.save() return render(request, 'add_article_form.html', {})
def edit_article(request, article_id): if request.method == 'GET': article = Article.objects.get(id=article_id) labels_in_article = article.labels.all() labels = Label.objects.all() return render( request, 'edit_article.html', { 'article': article, 'labels': labels, 'labels_in_article': labels_in_article }) elif request.method == 'POST': edit_article = Article.objects.get(id=article_id) edit_article.title = request.POST['article_title'] edit_article.raw_content = request.POST['article_raw_content'] edit_article.markdown_content = request.POST[ 'article_markdown_content'] edit_article.last_edit_time = timezone.now() edit_article.save() # 清楚所有关联label edit_article.labels.clear() # 重新绑定 labels = request.POST.getlist('labels[]') for label_id in labels: label = Label.objects.get(id=label_id) # label.number_is_used = label.number_is_used + 1 label.save() lia = LabelsInArticle(article=edit_article, label=label) lia.save() labels = Label.objects.all() labels_in_article = edit_article.labels.all() return render( request, 'edit_article_form.html', { 'article': edit_article, 'labels': labels, 'labels_in_article': labels_in_article })