DEV Community

Cover image for Django Signals part 1
Amr shadid
Amr shadid

Posted on • Edited on

Django Signals part 1

In this article, I will explain what Django built-in signals are. and where are they implemented? And why are they so effective and useful? Let's get started!

Django signal helps decoupled applications get notified when actions occur elsewhere in the framework.

This means that the function is notified to be executed when a certain condition is met. For instance when we add a new record to a database, Sometimes we would need to perform the function before or after saving, deleting and so on.

What can Django Signals do for us? Consider the following scenario: suppose we have a blog platform with many users, and if any user publishes a new article, all other users should be notified by sending a short notification such as "A new article has been added, check it out."

In this case, we can use Django signal to notify a function that is used to send a notification to all other users once the article has been successfully saved on the database.

wakeup!

Django comes with five built-in signals: Model, Management, HTTP requests, Testing, and Database wrappers. Each of those signals is an object from the signal class, which provides a set of functions for signal handling, such as connecting and disconnecting.

signals have two parameters that are commonly used: sender and receiver. We may specify who will send the signal and where we will receive it. Which function should be run?

In this part of our Django signals series, we'll look at request/response (HTTP) signals, explain their specifics, and demonstrate how to utilize them easily and correctly.

Request/response signals are used to work with HTTP requests to do a variety of things such as generate a performance report for the request or perform some operations when the request begins, when it is completed, or when an error occurs.

request/response signals:

  1. request_started signal
  2. Request_finished signal
  3. got_request_exception signal

To work with signals, the receiver function must have a '**Kwargs' parameter.

#request/response signals can import statement 
from django.core.signals import <#Name-Signal#>
Enter fullscreen mode Exit fullscreen mode

  1. request_started signal:

import statement:
from django.core.signals import request_started

When a request is initiated, this signal is used to execute a function. For example, when a request is initiated, start a timer.

Parameters:

-1 sender: In the default configuration, the sender of the request_started signal is WSGI.
-2 environ: The environ dictionary provided to the request


  1. request_finished signal

import statement:
from django.core.signals import request_finished

This signal is used to executed a function when request is finished. Follow to previous example we want to stop timer when request is finished.

request_finished have one parameter:

-1 sender: as above


  1. got_request_exception

This signal is used for request exceptions. For example, we want to save our failure logs to a text file.

import statement:
from django.core.signals import got_request_exception

request_finished have one parameter:

-1 sender: Not used (always None)
-2 request: The HTTPRequest object, provided by HTTPRequest class from Django framework,


Code :

import time 
import logging
from django.core.signals import request_started,\
    request_finished,\
    got_request_exception

def started(sender, **kwargs):
     global started
     started = time.time()

def finished(sender, **kwargs):
    total = time.time() - started
    print ("Timer| %.4fs" % total)

def log(**kwargs):
    logging.exception('error')

request_started.connect(started) 
request_finished.connect(finished) 
got_request_exception.connect.connect(log)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)