Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
159 stars 67 forks source link

Datatables crashes when has no "draw" param #98

Closed kalombos closed 5 years ago

kalombos commented 7 years ago

If i do not pass "draw" param in get request in flask example i get a 500 error. I can make a PR to fix it something like this:

    def output_result(self):
        """Output results in the format needed by DataTables."""
        output = {}
        try:
            output['draw'] = str(int(self.params['draw']))
        except Exception as exc:
            self.error = str(exc)

        output['recordsTotal'] = str(self.cardinality)
        output['recordsFiltered'] = str(self.cardinality_filtered)
        if self.error:
            output['error'] = self.error
            return output

        output['data'] = self.results
        for k, v in self.yadcf_params:
            output[k] = v
        return output

https://github.com/Pegase745/sqlalchemy-datatables/blob/master/datatables/__init__.py#L241

Should i make it?

magykjames commented 7 years ago

I'm having this issue with a Flask app, but using standard SQLAlchemy (not the Flask version). I can reproduce the error from the Python console, it happens when calling the .output_result() method.

In the JS it's this:

table = $('#inventory').DataTable({ "processing": true, "serverside": true, "ajax": "{{ url_for('dtinventory') }}" })

In the app I define the columns with ColumnDT(Table.columnname) like in the examples. I printed out request.args.todict() and it shows {'': u'1498955924433'}. The result gives me KeyError: 'draw'.

What is this 'draw' and how is that supposed to be passed? I can't find any examples using "ajax" you can even define specific parameters. I assume 'draw' probably has something to do with setting the number of rows to return?

I'm using DataTables JS version 1.10.13, SQLAlchemy 1.1.6, and sqlalchemy-datatables 1.2.0.

And kalombos, nice cat. :)

tdamsma commented 7 years ago

TBH I am not sure what the draw parameter does. Perhaps we should put a default value. Something like this should do it: output['draw'] = str(int(self.params['draw'])) to output['draw'] = str(int(self.params.get('draw',1))

eddyc05 commented 6 years ago

@magykjames were you able to find a solution? having the same issue

Cabalist commented 6 years ago

Don't know if this helps but I found I got this error when I was making requests to the data endpoint instead of letting the Datatables object make the ajax request. The Datatables JS will add in all the necessary params for output_result()

petri commented 6 years ago

See https://datatables.net/manual/server-side#Returned-data

siccovansas commented 5 years ago

I receive this error every now and then on my site https://openmultilaterals.org/. It happens with this call https://github.com/openstate/multilateral-organisations/blob/master/app/routes.py#L571.

I guess the suggested PR of @kalombos or the fix from @tdamsma would solve it?

tdamsma commented 5 years ago

@siccovansas, I kind of forgot about this, thanks for the reminder. Nice site by the way

siccovansas commented 5 years ago

Thanks for the fix everyone! Works like a charm :)