Пример #1
0
def edit_category(request, pk, template_name='jizhang/new_category.html'):
    out_errors = []
    if request.method == 'POST':
        form = CategoryForm(request, data=request.POST)
        if form.is_valid():
            form.save(pk)
            return HttpResponseRedirect("/jizhang/categories")
    else:
        category_list = get_object_or_404(Category, id=pk)
        form = CategoryForm(request, instance=category_list)
    return render(request, template_name, {'form': form})
Пример #2
0
def new_category(request):
    if request.method == 'POST' and request.is_ajax():
        form = CategoryForm(request, data=request.POST.copy())
        if form.is_valid():
            # 将数据保存到数据库
            clean_data = form.cleaned_data
            # print("clean_data: ", clean_data)
            # form.add_error('category_name', '该分类已经存在')
            # form.add_error('category_name', '第二个错误')
            # print("clean_data: ", clean_data)
            # print("clean_data: ", form.cleaned_data)
            # print("form.errors: ", form['category_name'].errors, type(form['category_name'].errors))
            # print("退出...")
            category_name = clean_data['category_name']  # 名称
            p_category = None if clean_data[
                'p_category'] == '0' else None  # 从数据库获取	# 父类名称	value即为id
            isIncome = bool(int(clean_data['isIncome']))  # 收入/支出
            u = request.user  # 用户
            c = Category(p_category=p_category,
                         name=category_name,
                         isIncome=isIncome,
                         user=u)
            try:
                c.save()  # 保存到数据库
            except IntegrityError:
                form.add_error('category_name', '该分类已经存在!')
            else:
                print("category保存成功")
                in_json = json.dumps([True, {}])  # 返回操作状态
                return HttpResponse(in_json, content_type='application/json')
        in_json = json.dumps([False, form.errors])
        return HttpResponse(in_json,
                            content_type='application/json')  # 表单验证不通过,返回错误信息
    else:
        form = CategoryForm(request)
    return render(request, "jizhang/new_category.html", {
        'form': form,
    })
Пример #3
0
def edit_category(request, c_id=None):
    '''
	0、修改的记录是c_id
	1、获取编辑的是哪一条记录
	2、使用提交的数据更新该条记录
	'''
    other_error = {}
    print(request.POST)
    try:
        c = Category.objects.get(id=c_id)  # 将要修改的记录
    except Category.DoesNotExist:
        print('不存在该记录')
    else:
        if request.method == 'POST' and request.is_ajax():
            form = CategoryForm(request, data=request.POST.copy())
            if form.is_valid():
                clean_data = form.cleaned_data
                c.name = clean_data['category_name']  # 修改为新的分类名称
                p_id = int(clean_data['p_category'])  # 获取提交的父类id
                try:
                    u_categories = Category.objects.filter(
                        user=request.user)  # 该用户的所有分类
                    u_p_id = c_id if c.p_category is None else c.p_category.id
                    childs_set = set()
                    childs_set = get_category_childs(childs_set, u_categories,
                                                     u_p_id)  # 获取该用户当前父类下所有子类

                    c.p_category = None if p_id == 0 else Category.objects.get(
                        id=p_id)  # 新的父类
                    if p_id == int(c_id) or c.p_category in childs_set:
                        form.add_error('p_category', '父类不能设置为自身和其子类')
                        in_json = json.dumps([False, form.errors])
                        return HttpResponse(in_json,
                                            content_type='application/json')
                except Category.DoesNotExist:
                    pass
                else:
                    c.isIncome = bool(int(clean_data['isIncome']))  # 新的收支情况
                    try:
                        c.save()
                    except IntegrityError:
                        form.add_error('category_name', '该分类已经存在!')
                    else:
                        print('category修改成功')
                        in_json = json.dumps([True, {}])
                        return HttpResponse(in_json,
                                            content_type='application/json')
            in_json = json.dumps([False, form.errors])
            return HttpResponse(in_json, content_type='application/json')

        else:
            category_name = c.name
            p_category = '0' if c.p_category is None else c.p_category.id
            isIncome = int(c.isIncome)
            form = CategoryForm(request,
                                data={
                                    'category_name': category_name,
                                    'p_category': p_category,
                                    'isIncome': isIncome
                                })
        return render(request, 'jizhang/new_category.html', {
            'form': form,
            'other_error': other_error
        })