63 lines
1.6 KiB
Plaintext
63 lines
1.6 KiB
Plaintext
![]() |
This rule enforces that the '@receiver' decorator is placed on top of all other decorators in Django functions.
|
||
|
|
||
|
|
||
|
== Why is this an issue?
|
||
|
|
||
|
In Django, the '@receiver' decorator is used to register signal handlers. These handlers are used to respond to events that occur in the application, such as a user logging in or a database record being saved.
|
||
|
|
||
|
|
||
|
The order in which decorators are applied can have a significant impact on their behavior. In the case of the @receiver decorator, it is important that it is applied first, before any other decorators, in order to ensure that the signal handler is registered correctly.
|
||
|
|
||
|
|
||
|
If the '@receiver' decorator is not applied first, the decorators placed above it will be ignored, which can result in unexpected behavior or even errors in the application.
|
||
|
|
||
|
== How to fix it
|
||
|
|
||
|
To fix this issue, simply move the '@receiver' decorator to the top of the list of decorators used to decorate the function.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,python]
|
||
|
----
|
||
|
from django.dispatch import receiver
|
||
|
from django.views.decorators.csrf import csrf_exempt
|
||
|
|
||
|
@csrf_exempt
|
||
|
@receiver(some_signal)
|
||
|
def my_handler(sender, **kwargs):
|
||
|
...
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,python]
|
||
|
----
|
||
|
from django.dispatch import receiver
|
||
|
from django.views.decorators.csrf import csrf_exempt
|
||
|
|
||
|
@receiver(some_signal)
|
||
|
@csrf_exempt
|
||
|
def my_handler(sender, **kwargs):
|
||
|
...
|
||
|
----
|
||
|
|
||
|
|
||
|
== Resources
|
||
|
|
||
|
=== Documentation
|
||
|
* https://docs.djangoproject.com/en/4.1/topics/signals/[Django signals]
|
||
|
|
||
|
|
||
|
ifdef::env-github,rspecator-view[]
|
||
|
|
||
|
'''
|
||
|
== Implementation Specification
|
||
|
(visible only on this page)
|
||
|
|
||
|
include::message.adoc[]
|
||
|
|
||
|
'''
|
||
|
|
||
|
endif::env-github,rspecator-view[]
|