Instruction: Define Django signals and provide examples of their usage.
Context: This question assesses the candidate's understanding of signals in Django, a framework feature for allowing decoupled applications to receive notifications when certain actions occur.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
For example, suppose we have an application where users can create posts. We might want to send an email notification to the user every time a post is created successfully. Instead of directly calling the email function after the post save logic, which can lead to tightly coupled code, we can emit a signal post-creation. Any part of our application that listens to this signal can then execute its logic, such as sending an email.
To implement signals in Django, you first define a signal by creating an instance of django.dispatch.Signal....