def add_choice(request): if request.method == 'POST': form = ChoiceAddForm(request.POST) if form.is_valid(): choice_problem = form.save(user=request.user) return redirect(reverse("choice_problem_detail", args=[choice_problem.id])) else: form = ChoiceAddForm() return render(request, 'choice_problem_add.html', {'form': form, 'title': '新建选择题'})
def update_choice_problem(request, id): problem = get_object_or_404(ChoiceProblem, pk=id) json_dic = {} # 知识点选择的需要的初始化数据 for point in problem.knowledgePoint2.all(): json_dic[point.id] = point.upperPoint.classname.name + ' > ' + point.upperPoint.name + ' > ' + point.name initial = {'title': problem.title, 'a': problem.a, 'b': problem.b, 'c': problem.c, 'd': problem.d, 'selection': problem.right_answer, 'classname': 0, 'keypoint': json.dumps(json_dic, ensure_ascii=False).encode('utf8') } # 生成表单的初始化数据 if request.method == "POST": # 当提交表单时 form = ChoiceAddForm(request.POST) if form.is_valid(): form.save(user=request.user, id=id) return redirect(reverse("choice_problem_detail", args=[id])) return render(request, 'choice_problem_add.html', {'form': ChoiceAddForm(initial=initial)})
def update_choice_problem(request, id): problem = get_object_or_404(ChoiceProblem, pk=id) if request.user != problem.creater and not request.user.is_admin: raise PermissionDenied json_dic = {} # 知识点选择的需要的初始化数据 for point in problem.knowledgePoint2.all(): json_dic[ point. id] = point.upperPoint.classname.name + ' > ' + point.upperPoint.name + ' > ' + point.name initial = { 'title': problem.title, 'a': problem.a, 'b': problem.b, 'c': problem.c, 'd': problem.d, 'selection': problem.right_answer, 'classname': 0, 'keypoint': json.dumps(json_dic, ensure_ascii=False).encode('utf8') } # 生成表单的初始化数据 if request.method == "POST": # 当提交表单时 form = ChoiceAddForm(request.POST) if form.is_valid(): form.save(user=request.user, id=id) return redirect(reverse("choice_problem_detail", args=[id])) return render(request, 'choice_problem_add.html', {'form': ChoiceAddForm(initial=initial)})