#12747: Custom HTTP status reason phrases are not supported
-------------------------------------+-------------------------------------
     Reporter:  Gustavo              |                    Owner:  gisle
         Type:  New feature          |                   Status:  new
    Component:  HTTP handling        |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  http status, http    |             Triage Stage:  Accepted
  status reason, http status reason  |      Needs documentation:  0
  phrase                             |  Patch needs improvement:  0
    Has patch:  1                    |                    UI/UX:  0
  Needs tests:  0                    |
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by Paul McNett <p@…>):

 This is hackish, quickly written, and tested for only 10 minutes or so but
 I implemented custom status codes by monkeypatching the
 django.core.handlers.wsgi.STATUS_CODE_TEXT dict like:

 {{{
 #!python
 import django.core.handlers.wsgi as wsgi

 class NextDict(dict):
     def __init__(self, *args, **kwargs):
         self._nextdict = {}
         super(NextDict, self).__init__(*args, **kwargs)

     def set_next(self, key, value):
         """
         The next time this key is queried, return the passed value.
         After that, use the default.
         """
         self._nextdict[key] = value

     def __getitem__(self, key):
         if key in self._nextdict:
             v = self._nextdict[key]
             del(self._nextdict[key])
             return v
         return super(NextDict, self).__getitem__(key)

 wsgi.STATUS_CODE_TEXT = NextDict(wsgi.STATUS_CODE_TEXT)
 }}}

 And then when I want my response to have a custom message for it's status
 code:

 {{{
 #!python
     class MyView(request):
         wsgi.STATUS_CODE_TEXT.set_next(200, "hello")
         return HttpResponse("hello again")
 }}}

 I googled for similar ideas and didn't find anyone doing this, so I
 thought it may give someone some ideas to develop this into a plugin or
 something. It wouldn't add overhead to the default (and reasonable)
 implementation, but would help some of us satisfy our requirements easily
 and without adding too much extra overhead...

-- 
Ticket URL: <https://code.djangoproject.com/ticket/12747#comment:16>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to