SmokeyD wrote:
Hey people,

sorry to bother you guys again. I have another question.
Is there a way to detect the tg_format argument to a controller
method?

Now, if I have a controller method, with @expose("json") which returns
a dict(form=DataGird(fields=fields),value=value)
and then call that controller with ?tg_format=json I get an error from
jsonify or something stating that the value is not jsonifiable (does
that word exist?;).
So instead I return
dict(form=DataGird(field=fields).render(value=value). In the template
I just use XML(form) to display the form if the controller is called
without tg_format=json.
I would rather detect in the controller though whether or not it is
called with tg_format=json  so I can decide in the controller if it
should return the rendered or unrendered form.

Do I make any sense? I hope somebody can help. Thanks in advance.


Here's two technique's for your problem:

1) This should do it. I use something similar for returning errors in one of my projects:
    ret_val = dict(value=value)
    if request.params.get('tg_format', 'html') == 'json':
        return ret_val
    ret_val['form']=DataGrid(fields=fields)

2) With my custom widgets I've taken to including an __json__() method that returns None. Since the data for the widget is being returned in a separate variable this means we just have an extra "widget:None" in the json return -- that's a lot less overhead than returning the html for the DataGrid. You can use this same technique by subclassing DataGrid and defining your own __json__() method:

class MyWidget(DataGrid):
    def __json__(self):
        return None

-Toshio

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to