Esempio n. 1
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,
    })
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
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})
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
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)
Esempio n. 9
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!'
            })
Esempio n. 10
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)
Esempio n. 11
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,
    })