robo,
I think you may be looking at this the wrong way.  The way I am using JSON
is to pass server generated data from django to my javascript callback on
the client. That is it.  when I make a request from the client to the server
I do things exactly like I would do a "Web 1.0" application.  When you go to
the index page of my app a YUI treeview is created on the left side of the
screen with various component names in the tree.  Clicking on any of the
component names will cause an async request to be made (AJAX) to django at a
url defined within urls.py just as a synchronous request, i.e.
'/add_tree_node/device'.  The difference is that the view that responds to
this url returns an HttpResponse that contains a JSON object instead of
html.  The javascript function processes the JSON object and changes the DOM
to add whatever markup is provided, alert the user, etc...  There is no time
where django needs to iterate through JSON directly.  The template for the
'index' page where this is happening has already been parsed by django and
that action is done.

The 'form' object that is returned is a standard
django.newforms.Formsubclass (or a form_for_model or
form_for_instance) and that template is
used only to essentially create an html snippet that can be displayed via
the javascript callback.  This process happens natively within django with
no JSON at all.  I am using a regular call to a model, i.e. d =
Device.objects.get(pk=1) to get the device object and f =
forms.form_for_instance(d) to get the form.  The render_to_response works
the same way, putting together html just like django always does.  The
JSONification happens with the simplesjson.dumps(response_dict)  and it is
pure javascript on the client callback to munge the returned data.

As far as why you are having problems with serializers I do not know, I have
only used simplejson.dumps.  I believe it is failing with your code because
it expects a  python dictionary, not a django queryset.
Try something like

data = Manager.objects.all()
managers = []
for d in data:
    managers.append({'data_attr1': d.attr1, 'data_attr2': d.attr2,
etc.....})
response_dict = {'Managers':  data}
return HttpResponse(simplejson.dumps(response_dict), mimetype="application/
javascript")

When you evaluate the JSON object with your javascript, the ouptut should
look like {Manageers:[{'data_attr1': d.attr1, 'data_attr2':
d.attr2},{'data_attr1':
d.attr1, 'data_attr2': d.attr2}]}
so using the YUI you would eval the response.ResponseText and end up with a
response_obj.Managers object that would contain the list of dictionaries.

Does this make sense?

-richard


On 9/19/07, robo <[EMAIL PROTECTED]> wrote:
>
>
> Richard, it still doesn't seem like Django can iterate through json.
> All we can do is stuff data into div tags. In the "{% if form %}"
> statement of yours, is "form" the object that you got from "var myobj
> = response_obj.form;" ? If it is, then you might have something
> special going on there that I can learn from.
>
> But meanwhile, I've tried to switch from serializers to simplejson,
> and it's giving me a "Error: bad http response code:500" everytime,
> whereas serializers give me the same error only 50% of the time (and
> this too is BAD!).
>
> When I'm using serializers, the only difference between getting an
> error not depends on which objects I'm getting. For example, I get an
> error when I use this code:
> def ajax_form_test(request):
>   data = serializers.serialize("json", Order.objects.all())
>   return HttpResponse(data, mimetype="application/javascript")
>
> but no errors when I use this code:
> def ajax_form_test(request):
>   data = serializers.serialize("json", Manager.objects.all())
>   return HttpResponse(data, mimetype="application/javascript")
>
> And when I use simplejson, I ALWAYS get errors no matter what. The
> code is as simple as this:
> def ajax_form_test(request):
>   data = Manager.objects.all()
>   return HttpResponse(simplejson.dumps(data), mimetype="application/
> javascript")
>
> Help me solve this bizzare mystery!
>
> Thanks,
>
> robo
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to