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)
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 })
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 })
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})
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)
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}
def addCategory(request): if request.method == 'POST': form = CategoryForm(request.POST, prefix='cat') if form.is_valid(): form.save() return redirect('/')
def test_categoryform_not_valid(self): params = dict(name='') form = CategoryForm(params) self.assertFalse(form.is_valid())
def test_categoryform_valid(self): params = dict(name='testname') form = CategoryForm(params) self.assertTrue(form.is_valid())