Skip to content

Commit 757968e

Browse files
committed
Added pattern
1 parent 9a5ec45 commit 757968e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

source/misc.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,20 @@ Instead of::
195195
The `.save()` should return a Model Object
196196

197197

198+
Code defensively in middleware and context processors.
199+
-----------------------------------------------------------
200+
Your middleware and context processors are going to be run for **all** requests.
201+
Have you handled all cases?
202+
203+
def process_request(request):
204+
if user.is_authenticated():
205+
profile = request.user.get_profile()#Hah, I create profiles during
206+
#registration so this is safe.
207+
...
208+
209+
Or it is? What about users created via `manage.py createsuperuser`, with the
210+
above middleware, the default user can not even access the admi site.
198211

212+
Hence handle all scenarios in middleware and context processors. This is one place
213+
where `try: .. except: ..` (bare except) block are acceptable. You do not wnat one
214+
middleare bringing down entire site.

0 commit comments

Comments
 (0)