def student_edit(request, group_id, student_id): instance = get_object_or_404(Students, id=student_id, group=group_id) form = StudentsForm(request.POST or None, instance=instance) if request.method == 'POST': if form.is_valid(): form.save() return redirect(reverse('group_list', args=[instance.group.id])) return TemplateResponse(request, 'student_add.html', {'form': form, 'group': instance.group})
def student_add(request, group_id): group = get_object_or_404(Group, id=group_id) form = StudentsForm(request.POST or None) if request.method == 'POST': if form.is_valid(): p = form.save(commit=False) p.group = group p.save() return redirect(reverse('group_list', args=[group.id])) return TemplateResponse(request, 'student_add.html', {'form': form, 'group': group})
def edit_students_or_groups(request): if 'stid' in request.GET and request.GET['stid']: group_instance = None grid = '' template = 'edit_student.html' try: stid = request.GET['stid'].encode('utf-8') student_instance = Students.objects.get(pk=stid) except: student_instance = None elif 'grid' in request.GET and request.GET['grid']: student_instance = None stid = '' template = 'edit_group.html' try: grid = request.GET['grid'].encode('utf-8') group_instance = Groups.objects.get(pk=grid) except: group_instance = None if request.method == "POST": if 'stud_ticket' in request.POST: form = StudentsForm(request.POST, instance=student_instance, auto_id=False) elif 'starosta' in request.POST: form = GroupsForm(request.POST, instance=group_instance, auto_id=False) response_dict = {} if request.is_ajax(): if form.is_valid(): form.save() response_dict['message'] = 'Changes have been saved' response_dict['result'] = 'success' else: response_dict['result'] = 'error' response = {} for error in form.errors: response[error] = form.errors[error][0] response_dict['response'] = response json = simplejson.dumps(response_dict, ensure_ascii=False) return HttpResponse(json, mimetype='application/json') else: if form.is_valid(): form.save() else: if student_instance: form = StudentsForm(instance=student_instance) elif group_instance: form = GroupsForm(instance=group_instance) return direct_to_template(request, template, {'form':form, 'stid':stid, 'grid': grid})
def student_edit(request, group_id, student_id): instance = get_object_or_404(Students, id=student_id, group=group_id) form = StudentsForm(request.POST or None, instance=instance) if request.method == 'POST': if form.is_valid(): form.save() return redirect(reverse('group_list', args=[instance.group.id])) return TemplateResponse(request, 'student_add.html', { 'form': form, 'group': instance.group })
def student_add(request, group_id): group = get_object_or_404(Group, id=group_id) form = StudentsForm(request.POST or None) if request.method == 'POST': if form.is_valid(): p = form.save(commit=False) p.group = group p.save() return redirect(reverse('group_list', args=[group.id])) return TemplateResponse(request, 'student_add.html', { 'form': form, 'group': group })
def add_new_student(request): if request.method == "POST": form = StudentsForm(request.POST, auto_id=False) response_dict = {} if request.is_ajax(): if form.is_valid(): form.save() response_dict['message'] = 'Changes have been saved' response_dict['result'] = 'success' else: response_dict['result'] = 'error' response = {} for error in form.errors: response[error] = form.errors[error][0] response_dict['response'] = response json = simplejson.dumps(response_dict, ensure_ascii=False) return HttpResponse(json, mimetype='application/json') else: if form.is_valid(): form.save() form = StudentsForm() return direct_to_template(request, "add_student.html", {'form':form})