예제 #1
0
def editCategoryView(request, update_id):
    author_request = Author.objects.filter(user__username=request.user)
    post_update = Category.objects.get(id=update_id)
    data = {
        'title': post_update.title,
    }
    post_form = CategoryForm(
        request.POST or None,
        initial=data,
        instance=post_update,
    )
    if request.method == 'POST':
        if post_form.is_valid():
            post_item = post_form.save()
            post_item.save()
            messages.success(request,
                             'Succesfully, category has been changed!')
            return redirect('accounts:listCategoryandAuthor')
        else:
            messages.error(
                request,
                'Error! You should check on some of those fields below.')
            return redirect('accounts:createCategory')
    else:
        context = {
            'page_title': 'Cryppy | Edit Category',
            'heading': 'Form | Edit Category',
            'post_form': post_form,
            'author': author_request,
        }
        return render(request, 'accounts/create_staff.html', context)
예제 #2
0
def category(request):
    """栏目"""
    if request.method == 'GET':
        categorys = Category.objects.all()
        return render(request, 'category.html', {'categorys': categorys})
    if request.method == 'POST':
        form = CategoryForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            Category.objects.create(**data)
            return HttpResponseRedirect(reverse('article:category'))
        else:
            categorys = Category.objects.all()
            return render(request, 'category.html', {
                'categorys': categorys,
                'form': form
            })
예제 #3
0
def category_update(request, id):
    """修改栏目"""
    if request.method == 'GET':
        category = Category.objects.filter(pk=id).first()
        return render(request, 'update-category.html', {'category': category})
    if request.method == 'POST':
        form = CategoryForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            Category.objects.filter(pk=id).update(**data)
            return HttpResponseRedirect(reverse('article:category'))
        else:
            categorys = Category.objects.all()
            return render(request, 'category.html', {
                'categorys': categorys,
                'form': form
            })
예제 #4
0
def category_edit(request, id):
    if not request.user.is_staff:
        return HttpResponse("无权限操作")
    a_cate = Category.objects.get(id=id)
    if request.method == 'POST':
        form = CategoryForm(request.POST, request.FILES, instance=a_cate)
        if form.is_valid():
            form_cd = form.cleaned_data
            a_cate.name = form_cd['name']
            a_cate.bio = form_cd['bio']
            a_cate.article_content = form_cd['article_content']
            if 'emblem' in request.FILES:
                a_cate.emblem = form.cleaned_data['emblem']
            a_cate.save()
            return redirect("article:category", a_cate.slug)
        else:
            return HttpResponse(form.errors)
    else:
        form = CategoryForm(instance=a_cate)

    return render(request, 'article/category_edit.html', {'form': form})
예제 #5
0
def createCategoryView(request):
    author_request = Author.objects.filter(user__username=request.user)
    post_form = CategoryForm(request.POST or None)
    if request.method == 'POST':
        if post_form.is_valid():
            post_item = post_form.save()
            post_item.save()
            messages.success(request,
                             'Succesfully, new category has been created!')
            return redirect('accounts:listCategoryandAuthor')
        else:
            messages.error(
                request,
                'Error! You should check on some of those fields below.')
            return redirect('accounts:createCategory')
    context = {
        'page_title': 'Cryppy | Create Category',
        'heading': 'Form | Create Category',
        'post_form': post_form,
        'author': author_request,
    }
    return render(request, 'accounts/create_staff.html', context)
예제 #6
0
파일: views.py 프로젝트: Ravillatypov/hdesk
def getBaseContext():
    categories = Category.objects.all()
    tags = Tag.objects.all()
    menu = []
    articles = Article.objects.all()
    for category in categories:
        category.items = []
        for art in articles:
            if category == art.category:
                category.items.append(art)
        menu.append(category)
    tagform = TagForm(prefix='tag')
    catform = CategoryForm(prefix='cat')
    return {'tags': tags, 'categories': categories, 'artmenu': menu}
예제 #7
0
파일: views.py 프로젝트: Ravillatypov/hdesk
def addCategory(request):
    if request.method == 'POST':
        form = CategoryForm(request.POST, prefix='cat')
        if form.is_valid():
            form.save()
    return redirect('/')
예제 #8
0
 def test_categoryform_not_valid(self):
     params = dict(name='')
     form = CategoryForm(params)
     self.assertFalse(form.is_valid())
예제 #9
0
 def test_categoryform_valid(self):
     params = dict(name='testname')
     form = CategoryForm(params)
     self.assertTrue(form.is_valid())