Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use django-datatable-view

I am currently building a project that will be displaying quite a bit of data in the Django admin I would like to substitute the list view used for datatables using django-datatable-view it says in the list for features that it can be dropped in as a replacement for list view. I might be not reading this correctly but from that I think it means totally replace list view so data tables are used by default which is what I want. I am new to Django and there seems to be no documentation on this add-on they have a few samples but no docs on how to actually use features they claim exist has anyone replaced the list view in Django with datatables using this add-on. I want to try and do it by default for all new models created but also the models like auth that I haven't extended yet.

like image 436
bobthemac Avatar asked Dec 26 '22 07:12

bobthemac


1 Answers

Well, I have tested this app on my localhost, here is some results(too much for comment, so I will answer here)

First, you need to take a look here: http://django-datatable-view.appspot.com/

It has got some documentation about how to implement django-datatable-view. For example: http://django-datatable-view.appspot.com/zero-configuration/ has got how to write a view to implement a table based on a model,

http://django-datatable-view.appspot.com/ordering/ has got how to get orders in table,

http://django-datatable-view.appspot.com/javascript-initialization/ has got information about js.

Better if you clone the repo and run it in localhost. There you will be able to experiment with the views and templates(as I tried to do/did).

In here: https://github.com/pivotal-energy-solutions/django-datatable-view/blob/master/datatableview/tests/example_project/example_project/example_app/views.py, you will see how multiple types of view(For no configuration table, specific column table etc) has been coded.

Second, what have I tried so far: My structure for this project was like this:

-Project
 manage.py
   -myapp(folder)
      views.py
      models.py
      urls.py
   -datatableview*(folder)
   -projectapp(folder)
      settings.py
      urls.py

*From cloned repo, I copied datatableview folder and pasted it in my project.

In myapp>models:

class Post(models.Model):
       title= models.CharField(max_length=150)
       body = models.TextField()
       created = models.DateField() 

In myapp>views:

class MyView(DatatableView):
    model = Post
    datatable_options = {
        'columns': [
            'title',
            'body',
            'created',
            ]
    }

In myapp>urls:

url(r'^$', MyView.as_view(),  name='myview'),

In templates: in (tempaltes/myapp/post_list.html)

{% block content %}
{{ datatable }}
{{ object_list }} 
{% endblock %}

Result was like this:

title   body    created
[<post: one >, <post: two>]

here title body created are names of table's column header.

PS: I know its not much of a help, but hopefully this information will help you go further. And a little recommendation, please take a look at django-tables2

like image 146
ruddra Avatar answered Dec 31 '22 13:12

ruddra