def subject_add(request):
    if request.method == 'POST':
        form = SubjectForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('subject'))
    else:
        form = SubjectForm()
    context = {
        'form': form
    }
    return render(request, 'appTemp/subject/subject_add.html', context=context)
예제 #2
0
def lab_3(request):
    if request.method == "POST":
        form = SubjectForm(request.POST)
        if form.is_valid():
            if form.cleaned_data['hour'] > 0:
                subject = form.save(commit=False)
                subject.name = form.cleaned_data['name']
                subject.hour = form.cleaned_data['hour']
                subject.semester = random.randint(1, 12)
                subject.save()
    else:
        form = SubjectForm()
    return render(request, 'main/lab_3.html', {'form': form})
def subject_edit(request, subject_id):
    if request.method == 'POST':
        subject = Subject.objects.get(pk=subject_id)
        form = SubjectForm(request.POST, instance=subject)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('subject'))
    else:
        subject = Subject.objects.get(pk=subject_id)
        fields = model_to_dict(subject)
        form = SubjectForm(initial=fields, instance=subject)
    context = {
        'form': form,
        'type': 'edit',
    }
    return render(request, 'appTemp/subject/subject_add.html', context)
예제 #4
0
    def post(self, request, id):
        subject = get_object_or_404(Subject, id=id)
        subject_form = SubjectForm(request.POST, instance=subject)

        if request.POST.get('delete_student'):
            student_id = request.POST['delete_student']
            student = Student.objects.get(id=student_id)
            subject.students.remove(student)
            subject_form = SubjectForm(instance=subject)
            subject_students = Student.objects.filter(
                subject__title=subject.title)
            no_subject_students = Student.objects.filter(subject=None)
            context = {
                'subject_form': subject_form,
                'subject': subject,
                'subject_students': subject_students,
                'no_subject_students': no_subject_students,
            }
            return render(request, 'subject_update.html', context=context)

        if request.POST.get('add_student'):
            student_id = request.POST['add_student']
            student = Student.objects.get(id=student_id)
            subject.students.add(student)
            subject_form = SubjectForm(instance=subject)
            subject_students = Student.objects.filter(
                subject__title=subject.title)
            no_subject_students = Student.objects.filter(subject=None)
            context = {
                'subject_form': subject_form,
                'subject': subject,
                'subject_students': subject_students,
                'no_subject_students': no_subject_students,
            }
            return render(request, 'subject_update.html', context=context)

        if subject_form.is_valid():
            subject_form.save()

        return redirect(reverse('subjects'))