def category_add(request): form_obj = CategoryForm() if request.method == 'POST': form_obj = CategoryForm(request.POST) if form_obj.is_valid(): form_obj.save() return redirect('category_list') title = '新增板块' return render(request, 'form.html', {'form_obj': form_obj, 'title': title})
def category_edit(request, pk): obj = models.Category.objects.filter(pk=pk).first() form_obj = CategoryForm(instance=obj) if request.method == 'POST': form_obj = CategoryForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() return redirect('category_list') title = '编辑分类' return render(request, 'form.html', {'form_obj': form_obj, 'title': title})
def category_change(request, pk=None): category_obj = models.Category.objects.filter(pk=pk).first() form_obj = CategoryForm(instance=category_obj) if request.method == 'POST': form_obj = CategoryForm(request.POST, instance=category_obj) if form_obj.is_valid(): form_obj.save() url = request.GET.get('url') if url: return redirect(url) return redirect('category_list') title = '编辑板块' if pk else '新增分类' return render(request, 'form.html', {'form_obj': form_obj, 'title': title})