from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404

from .forms import ReportForm, CopyFormSet
from .models import Report


@permission_required('reports.add_report', login_url='members:login')
def add_report(request):
    data = request.POST if request else None
    report_form = ReportForm(data)

    if report_form.is_valid():
        formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))
        report = report_form.save()
        request.session['report_in_creation'] = formset.instance = report
        if formset.is_valid():
            formset.save()
            del request.session['report_in_creation']

    return render(request, 'reports/add.html', locals())


def listing(request, page):
    reports_list = Report.objects.all()
    paginator = Paginator(reports_list, 30)

    reports = paginator.page(page)

    return render(request, 'reports/listing.html', {"reports": reports})


def show(request, id):
    report = get_object_or_404(Report, id=id)

    return render(request, 'reports/show.html', locals())

from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404, redirect

from .forms import ReportForm, CopyFormSet
from .models import Report


@permission_required('reports.add_report', login_url='members:login')
def add_report(request):
    data = request.POST if request.POST else None
    report_form = ReportForm(data)
    formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))

    if report_form.is_valid():
        report = report_form.save()
        request.session['report_in_creation'] = formset.instance = report
        if formset.is_valid():
            formset.save()
            del request.session['report_in_creation']
            return redirect('/members/profile/')

    return render(request, 'reports/add.html', locals())


def listing(request, page):
    reports_list = Report.objects.all()
    paginator = Paginator(reports_list, 30)

    reports = paginator.page(page)

    return render(request, 'reports/listing.html', {"reports": reports})


def show(request, id):
    report = get_object_or_404(Report, id=id)

    return render(request, 'reports/show.html', locals())

Fix the request.POST, usage of formset and redirect at the end
Fix the request.POST, usage of formset and redirect at the end

  from django.contrib.auth.decorators import permission_required
  from django.core.paginator import Paginator
- from django.shortcuts import render, get_object_or_404
+ from django.shortcuts import render, get_object_or_404, redirect
  
  from .forms import ReportForm, CopyFormSet
  from .models import Report
  
  
  @permission_required('reports.add_report', login_url='members:login')
  def add_report(request):
-     data = request.POST if request else None
+     data = request.POST if request.POST else None
      report_form = ReportForm(data)
+     formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))
  
      if report_form.is_valid():
-         formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))
          report = report_form.save()
          request.session['report_in_creation'] = formset.instance = report
          if formset.is_valid():
              formset.save()
              del request.session['report_in_creation']
+             return redirect('/members/profile/')
  
      return render(request, 'reports/add.html', locals())
  
  
  def listing(request, page):
      reports_list = Report.objects.all()
      paginator = Paginator(reports_list, 30)
  
      reports = paginator.page(page)
  
      return render(request, 'reports/listing.html', {"reports": reports})
  
  
  def show(request, id):
      report = get_object_or_404(Report, id=id)
  
      return render(request, 'reports/show.html', locals())
  
Fix the request.POST, usage of formset and redirect at the end
## Code Before:
from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404

from .forms import ReportForm, CopyFormSet
from .models import Report


@permission_required('reports.add_report', login_url='members:login')
def add_report(request):
    data = request.POST if request else None
    report_form = ReportForm(data)

    if report_form.is_valid():
        formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))
        report = report_form.save()
        request.session['report_in_creation'] = formset.instance = report
        if formset.is_valid():
            formset.save()
            del request.session['report_in_creation']

    return render(request, 'reports/add.html', locals())


def listing(request, page):
    reports_list = Report.objects.all()
    paginator = Paginator(reports_list, 30)

    reports = paginator.page(page)

    return render(request, 'reports/listing.html', {"reports": reports})


def show(request, id):
    report = get_object_or_404(Report, id=id)

    return render(request, 'reports/show.html', locals())

## Instruction:
Fix the request.POST, usage of formset and redirect at the end
## Code After:
from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator
from django.shortcuts import render, get_object_or_404, redirect

from .forms import ReportForm, CopyFormSet
from .models import Report


@permission_required('reports.add_report', login_url='members:login')
def add_report(request):
    data = request.POST if request.POST else None
    report_form = ReportForm(data)
    formset = CopyFormSet(data, instance=request.session.get('report_in_creation', Report()))

    if report_form.is_valid():
        report = report_form.save()
        request.session['report_in_creation'] = formset.instance = report
        if formset.is_valid():
            formset.save()
            del request.session['report_in_creation']
            return redirect('/members/profile/')

    return render(request, 'reports/add.html', locals())


def listing(request, page):
    reports_list = Report.objects.all()
    paginator = Paginator(reports_list, 30)

    reports = paginator.page(page)

    return render(request, 'reports/listing.html', {"reports": reports})


def show(request, id):
    report = get_object_or_404(Report, id=id)

    return render(request, 'reports/show.html', locals())
