#16398 closed New feature (invalid)
HttpResponse object should return self when write() method is called
Reported by: | Jonathan Sawyer | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
My use case:
# Current procedure def myview(req): resp = HttpResponse(mimetype='application/javascript') resp.write(json.dumps({'msg': 'Hello World!'})) return resp # Desired procedure def myview(req): resp = HttpResponse(mimetype='application/javascript') return resp.write(json.dumps({'msg': 'Hello World!'}))
HttpResponse.write()
returns None initially. It would be really nice if .write() would return self so I can return an HttpResponse
object. Instead I have to take up an extra line of code (and in my application, that is a lot when duplicated).
An extension on this request could be that all public methods on HttpResponse
should return self.
Attachments (1)
Change History (6)
by , 13 years ago
Attachment: | 16398.patch added |
---|
comment:1 by , 13 years ago
Attached is my patch. I have tested this functionality and it works when objects use HttpResponse as file objects. As far as I know, this won't break python compatibility because on errors, exceptions are raised rather than the output being the form of an error code. Thanks in advance.
comment:3 by , 13 years ago
Has patch: | set |
---|
comment:4 by , 13 years ago
Description: | modified (diff) |
---|
comment:5 by , 13 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
The write()
method follows the pattern of file-like objects, the write method for files returns None. This change would make one small use case easier, but is not at all an obvious thing for write
to return.
Patch to #16389 where self is returned on .write() method