Exemple #1
0
def article_new(request):
    if request.method == "POST":
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save()
            return redirect("magazine:article_detail", article.pk)
    else:
        form = ArticleForm()
    return render(request, "magazine/article_new.html", {"form": form})
Exemple #2
0
def article_edit(request, pk):
    form = Article.objects.get(pk=pk)
    if request.method == "POST":
        form = ArticleForm(request.POST, instance=form)
        if form.is_valid():
            article = form.save()
            return redirect("magazine:article_detail", article.pk)
    else:
        form = ArticleForm(instance=form)
    return render(request, "magazine/article_new.html", {"form": form})
Exemple #3
0
def article_edit(request, category_pk, article_pk):
    article = Article.objects.get(pk=article_pk)
    if request.method == 'POST':
       form = ArticleForm(request.POST, instance=article)
       if form.is_valid():
           article = form.save(commit=False)
           article.category = get_object_or_404(Category, pk=category_pk)
           article = form.save()
           return redirect('magazine.views.article_detail', category_pk, article_pk)
    else:
        form = ArticleForm(instance=article)
        return render(request, 'magazine/article_form.html', {'form': form, })
Exemple #4
0
def article_new(request, category_pk):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            article = form.save(commit=False)
            article.category = get_object_or_404(Category, pk=category_pk)
            article.save()
            messages.debug(request, 'New article posted')
            return redirect('magazine.views.article_detail', category_pk, article.pk)
    else:
        form = ArticleForm()
    return render(request, 'magazine/article_form.html', {'form': form, })