0
0
Djangoframework~20 mins

Task results and status in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view when a task is completed?

Consider a Django view that checks a task's status and returns a message accordingly.

from django.http import JsonResponse
from django.views import View

class TaskStatusView(View):
    def get(self, request, task_id):
        task = get_task_by_id(task_id)  # Returns a dict with 'status'
        if task['status'] == 'completed':
            return JsonResponse({'result': 'Task finished successfully'})
        else:
            return JsonResponse({'result': 'Task still running'})

What will be the JSON response if task['status'] is 'completed'?

Django
from django.http import JsonResponse
from django.views import View

class TaskStatusView(View):
    def get(self, request, task_id):
        task = {'status': 'completed'}
        if task['status'] == 'completed':
            return JsonResponse({'result': 'Task finished successfully'})
        else:
            return JsonResponse({'result': 'Task still running'})
A{"status": "completed"}
B{"result": "Task still running"}
C{"error": "Task not found"}
D{"result": "Task finished successfully"}
Attempts:
2 left
💡 Hint

Check the condition that matches the task status.

state_output
intermediate
2:00remaining
What is the value of task_status after running this Django signal handler?

Given this Django signal handler that updates a task's status after completion:

from django.dispatch import receiver
from django.db.models.signals import post_save

@receiver(post_save, sender=Task)
def update_task_status(sender, instance, **kwargs):
    if instance.is_finished:
        instance.status = 'completed'
        instance.save()

# Assume a Task instance with is_finished=True and initial status='pending'

What will be the status of the task after the signal runs?

Django
class Task:
    def __init__(self, is_finished, status):
        self.is_finished = is_finished
        self.status = status
    def save(self):
        pass

task = Task(is_finished=True, status='pending')
if task.is_finished:
    task.status = 'completed'
    task.save()
task_status = task.status
Afailed
Bpending
Ccompleted
DNone
Attempts:
2 left
💡 Hint

Check what happens when is_finished is True.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Django model field to track task status with choices?

You want to define a Django model field status that only allows 'pending', 'running', or 'completed'. Which code snippet is correct?

Astatus = models.CharField(max_length=10, choices=('pending', 'running', 'completed'))
Bstatus = models.CharField(max_length=10, choices=[('pending', 'Pending'), ('running', 'Running'), ('completed', 'Completed')])
Cstatus = models.CharField(max_length=10, choices={'pending': 'Pending', 'running': 'Running', 'completed': 'Completed'})
Dstatus = models.CharField(max_length=10, choices=[('pending'), ('running'), ('completed')])
Attempts:
2 left
💡 Hint

Choices must be a list of tuples pairing database value and human-readable name.

🔧 Debug
advanced
2:00remaining
Why does this Django async task status check raise an error?

Given this async function to check a task status:

async def check_task_status(task_id):
    task = Task.objects.get(id=task_id)
    if task.status == 'completed':
        return 'Done'
    else:
        return 'In progress'

When called in an async context, it raises an error. Why?

ADjango ORM methods like get() are synchronous and cannot be awaited in async functions
BThe function is missing an await keyword before Task.objects.get()
CThe task.status field is not accessible in async functions
DAsync functions cannot return strings in Django
Attempts:
2 left
💡 Hint

Consider how Django ORM works with async code.

🧠 Conceptual
expert
2:00remaining
Which Django feature best tracks task results and status asynchronously?

You want to run long tasks in the background and check their status and results later in Django. Which feature is best suited for this?

AUsing Celery with Django to run tasks asynchronously and store results
BUsing Django middleware to intercept and track task status
CUsing Django Channels to handle WebSocket connections for task updates
DUsing Django's built-in management commands to run tasks synchronously
Attempts:
2 left
💡 Hint

Think about background task queues and result storage.