Универсальная функция на Питоне: если article_id = 0 создаем запись 😝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def edit_action(request): title = request.POST.get('title','TITLE') content = request.POST.get('content','CONTENT') article_id= request.POST.get('article_id','0') if article_id == '0': models.Article.objects.create(title=title,content=content) articles = models.Article.objects.all() return render(request, 'blog/index.html', {'articles': articles}) else: article = models.Article.objects.get(pk=article_id) article.title = title article.content = content article.save() return render(request, 'blog/article_page.html', {'article': article}) |