Пример #1
0
def create_article(request): #saving article to database 
        form = ArticleForm(request.POST)
        context = {"form": form}
        if form.is_valid(): 
            form.save()
            return render(request, 'index.html', context)
        else:
            context = {"form": form}
            return render(request, 'create_article.html', context)
Пример #2
0
def create_article(request):
    form = ArticleForm(request.POST)
    form.save()
    return HttpResponseRedirect("/home")

# def post_show(request):
#     article = Article.objects.get(pk=id)
#     context = {'article': article}
#     return render(request, '')
Пример #3
0
def update_article(request, id):  # User updating an existing article.
    article = Article.objects.get(pk=id)
    form = ArticleForm(request.POST, instance=article)
    context = {"article": article, "form": form}
    if form.is_valid():
        form.save()
        return render(request, 'index.html', context)
    else:  # Else sends user back to existing article form.
        context = {"article": article, "form": form}
        return render(request, "edit_article_form.html", context)
Пример #4
0
def new_article(request):
    form = ArticleForm(request.POST)
    context = {'form': form}
    if request.method == 'POST':
        if form.is_valid():
            article = form.save(commit=False)
            article.user = request.user
            article.save()
            return HttpResponseRedirect('/home')
    return render(request, 'articleform.html', context)
Пример #5
0
def new_article(request):
    form = ArticleForm(request.POST)
    context = {"form": form, "message": "Create New Article", "action": "create_article"}
    if request.method == "POST":
        if form.is_valid():
            article = form.save(commit=False)
            article.user = request.user
            article.save()
            return HttpResponseRedirect("/home")
    # return render(request, 'show.html', context)
    
    return render(request, "form.html", context)
Пример #6
0
def edit(request, id):
    article = get_object_or_404(Article, pk=id, user=request.user.pk)
    form = ArticleForm(request.POST, instance=article)
    if request.method == 'POST':
        article = form.save(commit=False)
        article.user = request.user
        article.save()
        context = {'article': article}
        return render(request, 'article.html', context)
    else:
        context = {'article': article, 'form': form}
        return render(request, 'edit.html', context)
Пример #7
0
def edit_post_view(request, post_id):
    article = get_object_or_404(Article, pk=post_id)
    if not request.user.groups.filter(
            name='admins') and not (request.user.groups.filter(name='authors')
                                    and request.user == article.author):
        return render(request, 'blog/error.html', {
            'error': '权限不够',
            'message': '权限不够, 请联系管理员!',
        })
    if request.method == 'POST':
        article_form = ArticleForm(request.POST,
                                   request.FILES,
                                   instance=article)
        if article_form.is_valid():
            article = article_form.save(commit=False)
            article.is_markuped = False
            article.save()
            article_form.save_m2m()
            return HttpResponseRedirect(
                reverse('blog:single_post', args=(article.slug, )))
    else:
        article_form = ArticleForm(instance=article)
    return render(request, 'blog/write_post.html', {
        'article_form': article_form,
    })
Пример #8
0
def edit_article(request, id):
    article = get_object_or_404(Article, id=id, user=request.user.pk)
    article_form = ArticleForm(request.POST, instance=article)

    if article_form.is_valid():
        article_form.save()
        return redirect('post_details', id=id)
    else:
        return render(
            request, 'edit_article.html', {
                'article_form': article_form,
                'article.id': id,
                'error_msg': 'You have invalid form, try again!'
            })
Пример #9
0
def new_article(request):
    if request.user.has_perm('blog.create_article'):
        form = ArticleForm()
        context = {"form": form }
        response = render(request, 'create_article.html', context)
        return HttpResponse(response)
    else:
        return redirect(reverse('home_page'))
Пример #10
0
def create_new_article(request):
    # form = ArticleForm()
    # context = {'form': form}
    # response = render(request, 'new_article.html', context)
    # return HttpResponse(response)

    # form = ArticleForm(request.POST)
    # if form.is_valid():
    #     new_article = form.save()
    #     return HttpResponseRedirect('/home')
    # else:
    #     context = { 'form': form }
    #     response = render(request, 'new_article.html', context)
    #     return HttpResponse(response)
    if request.method == 'POST':
        article = Article(user=request.user)
        article.user = request.user
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = request.user
            instance.save()
            # instance.user = request.user
            # create_new_article = form.save()
            return HttpResponseRedirect('/home')
        else:
            context = {'form': form}
            response = render(request, 'new_article.html', context)
            return HttpResponse(response)
    else:
        form = ArticleForm()
        context = {'form': form}
        response = render(request, 'new_article.html', context)
        return HttpResponse(response)
Пример #11
0
def create_article(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.user = request.user
            new_article = form.save()
            return HttpResponseRedirect('/post/' + str(new_article.id))
    else:
        form = ArticleForm()
    response = render(request, 'create.html', {'form': form})
    return HttpResponse(response)
Пример #12
0
def create(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            new_article = form.save()
            return HttpResponseRedirect('/article/' + str(new_article.pk))

    form = ArticleForm()
    return render(request, 'create_article.html', {'form': form})
Пример #13
0
def write_post_view(request):
    if not request.user.groups.filter(name='authors'):
        return render(request, 'blog/error.html', {
            'error': '权限不够',
            'message': '权限不够, 请联系管理员!',
        })
    if request.method == 'POST':
        article_form = ArticleForm(request.POST, request.FILES)
        if article_form.is_valid():
            article = article_form.save(commit=False)
            article.author = request.user
            print 'In write_post_view', article.id
            article.save()
            article_form.save_m2m()
            return HttpResponseRedirect(
                reverse('blog:single_post', args=(article.slug, )))
    else:
        article_form = ArticleForm()
    return render(request, 'blog/write_post.html', {
        'article_form': article_form,
    })
Пример #14
0
def edit_blog_post(request, id):
    article = get_object_or_404(Article, pk=id, user=request.user)
    if request.method == 'GET':
        form = ArticleForm(instance=article)
        context = {'form': form, 'article': article}
        response = render(request, 'edit_post.html', context)
        return HttpResponse(response)
    elif request.method == 'POST':
        form = ArticleForm(request.POST, instance=article)
        if form.is_valid():
            updated_article = form.save()
            return HttpResponseRedirect('/post/{}'.format(article.id))
        else:
            context = {'form': form, 'article': article}
            response = render(request, 'edit_post.html', context)
            return HttpResponse(response)
Пример #15
0
def new_article(request):
    article = Article.objects.get(id=request.POST['article'])
    new_article = ArticleForm(request.POST)
    instance.user = request.user
    new_article.save()
    return redirect('post_details', id=article.id)
Пример #16
0
def edit_article(request, id):  # Renders a form to edit an existing article.
    article = Article.objects.get(pk=id)
    form = ArticleForm(instance=article)
    context = {"article":article, "form": form}
    return render(request, "edit_article_form.html", context)
Пример #17
0
def post_article(request, id):
    article = Article.objects.get(id=id)
    new_article = Article(article=article)
    form = ArticleForm(instance=new_article)
    context = {'form': form, 'post': Article.objects.get(pk=id)}
    return render(request, 'new_article.html', context)