tags:

views:

24

answers:

1

Can please anybody explain how to use datagrid in a django application????

A: 

You could use the djblets datagrid module for this.

A sample application that shows a datagrid of books is outlined below.

dgrid_test/
    books/
        __init__.py
        admin.py
        datagrids.py
        models.py
        urls.py
        views.py
        books.html


    __init__.py
    base.html
    manage.py
    settings.py
    urls.py

settings.py snippets:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'djblets.siteconfig.context_processors.siteconfig',
    'djblets.util.context_processors.settingsVars',
    'djblets.util.context_processors.siteRoot',
    'djblets.util.context_processors.ajaxSerial',
    'djblets.util.context_processors.mediaSerial',
)

TEMPLATE_DIRS = (
    '/path/to/dgrid_test'
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'djblets.datagrid',
    'djblets.feedview',
    'djblets.siteconfig',
    'djblets.util',
    'djblets.webapi',
    'dgrid_test.books'
)

models.py:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()
    def __str__(self):
    return '%s (%s, %s, %s)' % (self.name, self.city, self.state_province, self.country)

class Author(models.Model):
    salutation = models.CharField(max_length=10)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()
    def __str__(self):
    return '%s. %s %s' % (self.salutation, self.first_name, self.last_name)

class Book(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
    def __str__(self):
    return '%s %s' % (self.name, self.publication_date)

datagrids.py:

from models import Book
from djblets.datagrid.grids import Column, DataGrid

class BookDataGrid(DataGrid):
    name = Column("Name", sortable=True, link=True)
    publisher = Column("Publisher", sortable=True, link=True)
    publication_date = Column("Publication Date", sortable=True)

    def __init__(self, request):
            DataGrid.__init__(self, request, queryset=Book.objects.all(), title="Book")
            self.default_sort = ['name']
            self.default_columns = ['name', 'publisher', 'publication_date']

admin.py:

from dgrid_test.books.models import Book, Publisher, Author
from django.contrib import admin

admin.site.register(Book)
admin.site.register(Publisher)
admin.site.register(Author)

urls.py:

from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
     (r'^books/', include('dgrid_test.books.urls')),
     (r'^admin/', include(admin.site.urls)),
)

books/urls.py:

from django.conf.urls.defaults import *
from django.conf import settings

urlpatterns = patterns('',
    (r'^$', 'books.views.book_list'),
)

books/views.py:

from books.datagrids import BookDataGrid

def book_list(request, template_name='books/books.html'):
     return BookDataGrid(request).render_to_response(template_name)

base.html:

{% block content %}{% endblock %}

books/books.html:

{% extends "datagrid/datagrid.html" %}

There are still things to be done, like adding javascripts and images, but this should give you an initial working version.

You might also find this tutorial useful.

the_void