可以请任何人解释如何在django应用程序中使用datagrid ????
答案 0 :(得分:5)
您可以使用djblets datagrid 模块。
下面概述了一个显示书籍数据网格的示例应用程序。
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 片段:
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)),
)
<强>书籍/ urls.py 强>:
from django.conf.urls.defaults import *
from django.conf import settings
urlpatterns = patterns('',
(r'^$', 'books.views.book_list'),
)
<强>书籍/ 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.html 强>:
{% extends "datagrid/datagrid.html" %}
还有一些事情要做,比如添加javascripts和图像,但这应该会给你一个初始的工作版本。
您可能还会发现此tutorial有用。