using vars() function in django

12 views
Skip to first unread message

rafiee.nima

unread,
Aug 12, 2012, 10:15:32 AM8/12/12
to django...@googlegroups.com
Hi I need to to assign dynamic name to a variable I use blow code :
 for item,value in request.POST.items():
     if item=="kid_room"
        vars()[item]="ok"
        list.append({"a":kid_room})
        data=simplejson.dumps(list,cls=DjangoJSONEncoder)
        return HttpResponse(data, mimetype="application/json")

which kid_room is
request.POST item
but i got error that kid_room is not defined which means
vars()[item]="ok" dose not work
please help





Masklinn

unread,
Aug 12, 2012, 10:29:22 AM8/12/12
to django...@googlegroups.com
Why the hell would you want to do something like that in the fist place? How about jus writing:

if item == 'kid_room':
list.append({'a': 'ok'})

? If the name is hardcoded there obviously is no need to dynamically assign it, and if it's not you can just use a dict.

I see no reason to even try to do that, and so far I've never seen one when coding.
Reply all
Reply to author
Forward
0 new messages