def update(request): id_subject = request.GET.get('id', None) if not id_subject: return redirect('admin/subject') info = { 'info': { 'title': 'Update Subject - Admin Training Framgia', 'sidebar': ['subject'] }, 'data': SubjectForm(instance=Subject.objects.get(id=id_subject)), 'list_course': Course.objects.all(), 'list_course_selected': Subject.objects.get(id=id_subject).course.all().values_list('id', flat=True), 'status': { 'alert': '', 'message': '' } } if request.method == 'POST': subject_form = SubjectForm(request.POST, instance=Subject.objects.get(id=id_subject)) if subject_form.is_valid(): subject = subject_form.save(commit=False) subject.update_at = datetime.now() subject.course = subject_form.cleaned_data['course'] subject.save() info['status']['alert'] = 'info' info['status']['message'] = 'Update subject success !' else: info['data'] = subject_form return render(request, 'backend/subject/subject_update.html', info)
def create(request): info = { 'info': { 'title': 'Create Subject - Admin Training Framgia', 'sidebar': ['subject'] }, 'data': SubjectForm(), 'list_course': Course.objects.all(), 'status': { 'alert': '', 'message': '' } } if request.method == 'POST': subject_form = SubjectForm(data=request.POST) if subject_form.is_valid(): subject = subject_form.save(commit=False) subject.created_at = datetime.now() subject.update_at = datetime.now() subject.save() subject.course = subject_form.cleaned_data['course'] subject.save() info['status']['alert'] = 'info' info['status']['message'] = 'Create subject success !' else: info['data'] = subject_form info['list_course_selected'] = list(map(int, request.POST.getlist('course'))) return render(request, 'backend/subject/subject_create.html', info)