Opened 10 years ago
Closed 7 years ago
#23977 closed New feature (fixed)
Add setdefault() to HttpResponse
Description ¶
HttpResponse acts like a dictionary with respect to headers. It's a common middleware pattern to set headers unless they've been explicitly set by the view. eg.
class MyMiddleware(object): def process_response(self, request, response): if not 'X-Test' in response: response['X-Test'] = 12345 return response
Having a setdefault()
implementation would simplify this (not much in the trivial one-header case, but for example CORS middleware where you're setting a number of headers)
class MyMiddleware(object): def process_response(self, request, response): response.setdefault('X-Test', 12345) return response
Change History (6)
comment:1 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 10 years ago
Has patch: | set |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:4 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:5 by , 7 years ago
Cc: | added |
---|---|
Resolution: | fixed |
Status: | closed → new |
HttpResponseBase.setdefault should return a value in order to be consistent with dict.setdefault and what users expect from a function called setdefault on an objects, that behaves like dict.
comment:6 by , 7 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Please open a new ticket rather then reopening an existing one that's been fixed for three years. You can provide patches as attachments rather than as comments.
In 059c9ab24c41e1460fd8b7af65ea8d5f80f1aa82: