from django.utils import timezone


def get_requested_time(request):
    query = request.GET
    month, year = query.get('month', None), query.get('year', None)

    if month and year:
        return timezone.datetime(year=int(year), month=int(month), day=1)
    else:
        return None

from django.utils import timezone


def get_requested_time(request):
    query = request.GET
    month, year = query.get('month', None), query.get('year', None)

    if month and year:
        try:
            return timezone.datetime(year=int(year), month=int(month), day=1)
        except ValueError:
            pass
    return None

Fix handling of invalid parameters
Fix handling of invalid parameters

  from django.utils import timezone
  
  
  def get_requested_time(request):
      query = request.GET
      month, year = query.get('month', None), query.get('year', None)
  
      if month and year:
+         try:
-         return timezone.datetime(year=int(year), month=int(month), day=1)
+             return timezone.datetime(year=int(year), month=int(month), day=1)
-     else:
+         except ValueError:
+             pass
-         return None
+     return None
  
Fix handling of invalid parameters
## Code Before:
from django.utils import timezone


def get_requested_time(request):
    query = request.GET
    month, year = query.get('month', None), query.get('year', None)

    if month and year:
        return timezone.datetime(year=int(year), month=int(month), day=1)
    else:
        return None

## Instruction:
Fix handling of invalid parameters
## Code After:
from django.utils import timezone


def get_requested_time(request):
    query = request.GET
    month, year = query.get('month', None), query.get('year', None)

    if month and year:
        try:
            return timezone.datetime(year=int(year), month=int(month), day=1)
        except ValueError:
            pass
    return None
