Django

Code

Ticket #5138 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

Add __contains__ to HttpRequest to make it more dict-like

Reported by: anonymous Assigned to: nobody
Milestone: Component: Uncategorized
Version: SVN Keywords:
Cc: Triage Stage: Design decision needed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If I try to evaluate the view method

def index(request):
    if 'foo' in request: ...

I the error "0 not found in either POST or GET." Adding the following method to HttpRequest

def __contains__(self, key):
    return self.has_key(key)

fixes that problem. Please add that method to make the request behavior more dict-like.

Attachments

HttpRequest-contains.patch (0.5 kB) - added by robbie@prelab.net on 09/04/07 12:46:43.
Patch to add contains method to HttpResquest?

Change History

08/12/07 05:08:50 changed by Simon G. <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • summary changed from in operator doesn't work with HttpRequest to Add __contains__ to HttpRequest to make it more dict-like.
  • needs_tests changed.
  • needs_docs changed.

08/13/07 12:43:20 changed by anonymous

09/04/07 12:45:26 changed by robbie@prelab.net

  • has_patch set to 1.

This caught me out as well today. I think it's a good idea to enable, and then promote, the use of "in" over the "has_key" method since dict.has_key() is being depreciated.

I'm also attaching a patch to achieve this.

09/04/07 12:46:43 changed by robbie@prelab.net

  • attachment HttpRequest-contains.patch added.

Patch to add contains method to HttpResquest?

09/11/07 08:39:26 changed by russellm

(In [6097]) Fixed #5138 -- Added contains method to HttpRequest, mirroring existing has_key method. Thanks for the patch, robbie@prelab.net. NOTE: Original checkin message incorrectly referenced bug #5318.

09/11/07 08:39:33 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

09/11/07 09:04:40 changed by russellm

(In [6098]) Refs #5138 -- Refactored implementation of contains in HttpRequest introduced in [6097] after a suggestion from Malcolm. Applied a similar refactor for MergeDict? and Context which had comparable behavior.


Add/Change #5138 (Add __contains__ to HttpRequest to make it more dict-like)




Change Properties
Action