QueryDict is used to handle lists (as dictionaries) in GET/POST forms.
Question: How is QueryDict set? Documentation just refers to it: q = QueryDict('a=1'). How does it know where to get info?
Django foo never uses QueryDict directly:
1 | def handle(request): |
2 |
if request.method = = 'POST' : |
3 |
artists = request.POST.getlist( 'artists' ) # now artists is a list of [1,2,3] |
Really? I guess so. Appendix H says GET and POST are "dictionary-like" objects. Then how does this code work?
cur_cat = int(request.POST['CategorySelect'])
Does it convert the first key into an integer? No, that fails.
So request.POST['CategorySelect'] must not be a dictionary. In fact, if post is a dictionary, then request.POST['CategorySelect'] is a value from a dictionary.
So POST is a dictionary of item name vs. item value? D'oh! Now the QueryDict doc makes more sense: "QueryDict is a dictionary-like class customized to deal with multiple values for the same key." Django foo's example above makes more sense too. Finally, Appendix H has a full example at the end that makes this all clear!
C:\xampp\django\avppng>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dict = {1:'a', 2:'b'}
>>> dict
{1: 'a', 2: 'b'}
>>> int(dict)
Traceback (most recent call last):
File "", line 1, in
TypeError: int() argument must be a string or a number, not 'dict'
>>>
So request.POST['CategorySelect'] must not be a dictionary. In fact, if post is a dictionary, then request.POST['CategorySelect'] is a value from a dictionary.
>>> dict3 = {'cs':'1', 'junk':'2'}
>>> dict3
{'cs': '1', 'junk': '2'}
>>> int(dict3['cs'])
1
So POST is a dictionary of item name vs. item value? D'oh! Now the QueryDict doc makes more sense: "QueryDict is a dictionary-like class customized to deal with multiple values for the same key." Django foo's example above makes more sense too. Finally, Appendix H has a full example at the end that makes this all clear!
No comments:
Post a Comment