My code will seem like:
def _get_data(request, obj):
if obj.icon:
icon = '<img class="border" src="/site_media/%s" alt="%s"/>' %
(obj.get_icon_url(), obj.title)
else:
icon = '<img class="border"
src="/site_media/img/book_icon.jpg" alt="%s"/>' % obj.title
authors = [x.username for x in obj.authors.all()]
return ({'id':obj.id, 'icon':icon, 'title':obj.title,
'description':obj.description, 'author':','.join(authors),
'modifydate':obj.modifydate.strftime("%b %d,%Y %I:%m %p")})
def ajax_list(request):
result = []
objs = Book.objects.all()
for o in objs:
result.append(_get_data(request, o))
return json_response(result)
So I'd like write my own data gather method, and it can return other
data just like html snippets except for field value.
And for json method should be:
def json_response(data):
encode = settings.DEFAULT_CHARSET
return HttpResponse(simplejson.dumps(uni_str(data, encode)))
def uni_str(a, encoding=None):
if not encoding:
encoding = settings.DEFAULT_CHARSET
if isinstance(a, (list, tuple)):
s = []
for i, k in enumerate(a):
s.append(uni_str(k, encoding))
return s
elif isinstance(a, dict):
s = {}
for i, k in enumerate(a.items()):
key, value = k
s[uni_str(key, encoding)] = uni_str(value, encoding)
return s
elif isinstance(a, unicode):
return a
elif isinstance(a, (int, float)):
return a
elif isinstance(a, str) or (hasattr(a, '__str__') and
callable(getattr(a, '__str__'))):
if getattr(a, '__str__'):
a = str(a)
return unicode(a, encoding)
else:
return a
You can put json_response and uni_str into a module file. So I think
the work will be not so hard.
--
I like python!
UliPad <<The Python Editor>>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---